How to send request HTTP/POST with Query Params

I have some request HTTP/POST with Query Params request with follow format :
curl --location --request POST ‘http://.localhost:8080/autopay/api/v1/topup/payment?receiverCode=84354423273&balance=3000’
–header ‘accept: application/json’

and I have try to send with res.post with follow code :

var params={
headers: {
“accept”: “application/json”
}
};
var body=JSON.stringify(
{
});
let res=http.post(“http://.localhost:8080/autopay/api/v1/topup/payment?receiverCode=84354423273&balance=3000”," ",params);

But I cannot send this request because of invalid request (HTTP 400) so if somebody have met this issues before please help me.

Hi,

HTTP 400 is Bad Request, so that depends on what your service is expecting, but a few comments about your script:

  • It’s a bit awkward for a POST endpoint to receive query string arguments, but you’re passing them correctly here.
  • You’re sending an Accept header, but no Content-Type. This would be my guess as the reason for the 400, as your service probably requires Content-Type: application/json, so make sure you’re setting it.
  • Your body var is unused.

See the documentation for details.

HTH,

Ivan

1 Like

Hi Imiric ,
In my scripts , my body var is unused , and of course 400 is Bad request , I have sent this request on Postman and this work on Postman , and my cURL is generated from Postman. But in my case if POST with query params How I have sent request with query params?!
And with documentation, this for normally Json body request not for Query params request, with Query params request the body value will not using. But when I have sent with res.post(), I have set the body params , so that reason why 400 is appears.
So what should I do with query params, and if you have any examples with this , pls provides me, because I have read the documentation too and search via google and no answer.

Hi,

I’m not sure how to help you as your k6 script looks fine. You’re correctly passing all query string arguments.

Like I said, a 400 response is an application error, and only you as the user of that web service will know what’s wrong with the request. For example: do you get an error reason in the response body? You could manually inspect it, or use --http-debug to see what you’re sending and receiving. You could also use a packet capture software like Wireshark to troubleshoot this.

My best guess is not including Content-Type: application/json, but you say it works with curl and Postman, so something else could be missing.

Good luck!

1 Like