vscode-restclient icon indicating copy to clipboard operation
vscode-restclient copied to clipboard

JSON body params not recognized

Open satyakvv opened this issue 5 years ago • 11 comments

I have the following REST API to try in VS Code REST client. It returns the error "AADSTS900144: The request body must contain the following parameter: 'grant_type'" . I looks like the grant_type json element is not passed to the server. The same request works fine in Postman.

###======== HTTP request below ======= POST https://accounts.accesscontrol.windows.net//tokens/OAuth/2 HTTP/1.1 content-type: application/json

{ "grant_type":"client_credentials", "client_id":"", "client_secret":"", "resource":"" } ###========= end of HTTP request ====

satyakvv avatar May 04 '20 22:05 satyakvv

Same. Here is my workaround.

POST /tokens/OAuth/2 HTTP/1.1
Host: https://accounts.accesscontrol.windows.net
Content-Type: application/json

{
    "grant_type":"client_credentials",
    "client_id":"",
    "client_secret":"",
    "resource":""
}

secsilm avatar May 11 '20 03:05 secsilm

@satyakvv

POST https://accounts.accesscontrol.windows.net//tokens/OAuth/2 HTTP/1.1

There is an extra / in your request URL, could you please try again after removing it?

Huachao avatar May 11 '20 03:05 Huachao

@secsilm what's your problem when you use your original way?

Huachao avatar May 11 '20 04:05 Huachao

@secsilm what's your problem when you use your original way?

Backend got empty request body. But now it worked using my original way. Maybe there were some mistakes.

Thanks.

secsilm avatar May 12 '20 04:05 secsilm

i've been noticing a similar issue as well. i'm running a local server and the body is never sent along with the request.

example:

POST http://localhost:3001/x/y
Content-Type: "application/json"

{
  "something": "else"
}

no body is received in the server.

edit: i've just noticed the issue 🤦‍♂️ application/json cannot be wrapped in quotes, so the header should be Content-Type: application/json

takecare avatar May 19 '20 16:05 takecare

POST http://localhost/rest-php/api/post/create.php

Content-Type: application/json

{
    "title": "first post",
    "category_id": "1",
    "body": "This is a sample post"
}

###

getting null content in server. "Notice: trying to get property 'title' of non-object"

minhazmiraz avatar Dec 30 '20 18:12 minhazmiraz

After removing the new line next to POST, it worked properly.

POST http://localhost/rest-php/api/post/create.php
Content-Type: application/json

{
    "title": "first post",
    "category_id": "1",
    "body": "This is a sample post"
}

###

minhazmiraz avatar Dec 30 '20 18:12 minhazmiraz

I also added to server.js app.use(express.urlencoded({ extended: true })); now works as expected

Dzirt2006 avatar Apr 15 '21 17:04 Dzirt2006

Try adding this to server.js: app.use(express.json());

LozynskiW avatar Apr 24 '21 21:04 LozynskiW

Try adding this to server.js: app.use(express.json());

Thanks, this was my issue, my server.js was missing this

sir-prem avatar May 19 '21 15:05 sir-prem

I am also having this issue. Of note:

  • I just migrated to a new M1-based MacBook (from an Intel-based MacBook)
  • the POST was working fine on the old MacBook
  • The post fails with 'Missing query'.... but I can right-click and 'Copy Request as cURL', paste it on the terminal, and it works fine.
POST https://api-us.securitycenter.windows.com/api/advancedqueries/run
Authorization: Bearer {{authDefender.response.body.access_token}}
Content-Type: application/json
Accept: application/json
Accept-Encoding: gzip

{ "Query":"DeviceInfo  
| where DeviceName contains 'ahostname'
"}

Another note- it works fine if I remove the newlines in the string like this:

{ "Query":"DeviceInfo  | where DeviceName contains 'ahostname'"}

Like I said, this POST was working just fine on my old MacBook, but on the new (M1) MacBook it will not accept the newlines within the json string.

Technically JSON with a newline would need to be escaped. I think the REST Client was doing that behind the scenes on the old one? And it IS working when copied as cURL (new lines are stripped). So...

iamcaje-psh avatar Aug 30 '22 16:08 iamcaje-psh