question

lesniermarrero avatar image
lesniermarrero asked

http.request not sending the content body

Hi,this is my code.i have tryied almost anything and it doenst work. var url = "https://facilservicios.com/servicioDesarrollo.php"; var method = "post"; var contentBody={ }; contentBody["metodo"] = "ListaArticulosNetgamez"; contentBody["ID"] =_token; contentBody["Proyecto"] ="Quantum"; var contentType = "application/x-www-form-urlencoded"; var headers = {}; var responseString = http.request(url, method, JSON.stringify(contentBody), contentType,headers);

the endpoint responds with bad request because body content is not reconized by the end point,i tryied it on postman and c# httpclient and it works fine.

CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Simon Cui avatar image
Simon Cui answered

The issue is caused by mismatched of content type and content body. Currently, you are using content type of "application/x-www-form-urlencoded", but your content body is json format. To resolve this issue, you can modify content body to var contentBody="metodo=ListaArticulosNetgamez&ID=***********&Proyecto=Quantum";. Or you can modify the content type to var contentType = "application/json";, such that content type and body could match.

Here is the test function:

     handlers.PostHttpRequest = function (args, context) {
         var url = "https://facilservicios.com/servicioDesarrollo.php"; 
         var method = "post"; 
         var contentBody="metodo=ListaArticulosNetgamez&ID=***********&Proyecto=Quantum"; 
         var contentType = "application/x-www-form-urlencoded"; 
         var headers = {}; 
         var responseString = http.request(url, method, contentBody, contentType,headers);
         log.info("responseString: " +  responseString);
     }
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

lesniermarrero avatar image lesniermarrero commented ·

IIt works fine.Thank you very much,i had never used that contetnt type before.Now i can continue with develpment.Please erase all trace of the token .i dont know if that is a testing one or a real one.it swas provided by the service.

0 Likes 0 ·
Simon Cui avatar image Simon Cui lesniermarrero commented ·

Glad to hear it worked, I will remove it then.

0 Likes 0 ·
Simon Cui avatar image
Simon Cui answered

I tested your code in Cloud Script and it returned {"Error":1,"ErrorDescripcion":"Ver manual de uso"}, which is as same as the result when I clicked the URL directly in browser.

5802-p1.png

I also tested to configure Postman referring to your codes, which returned same response as above. I cannot reproduce your issue with error of “bad request because body content is not recognized by the end point”. Could you share more detailed information?


p1.png (25.7 KiB)
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

lesniermarrero avatar image lesniermarrero commented ·

Yeah.that is the error i get. Working with a dev of that api he told me that my request is not sending the body.but the actual response is that one,this is the token needed for the request. Token:"???? ".thanks in advance,i cant go any further because of this.

0 Likes 0 ·
lesniermarrero avatar image lesniermarrero commented ·

I also tryied it with c# httpclient and it works fine too.in clodscript is the only place it doenst work.please check it out and tell me if it is a bug or an error in my code.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.