question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending JSON API POST request.

See original GitHub issue

Do you have a plan to allow (or support) JSON API?

Per the JSON API spec, I have to set 'Content-Type': 'application/vnd.api+json', and it looks like it is overridden by application/json.

 api.postJsonApi = function(url, data) {
            return this.post(url, data, {
                headers: {
                    'Content-Type': 'application/vnd.api+json'
                }
            });
        };

api is an instance of axios. In the request header, it becomes application/json;charset=UTF-8, probably, it is because the data is an object.

I set transformRequest, but I still could not make it work.

api.postJsonApi = function(url, data) {
            return this.post(url, data, {
                transformRequest: [function(data, headers) {
                    headers['Content-Type'] = 'application/vnd.api+json';
                    return data;
                }]
            });
        };

api.postJsonApi = function(url, data) {
            return this.post(url, data, {
                transformRequest: [function(data, headers) {
                    headers.post['Content-Type'] = 'application/vnd.api+json';
                    return data;
                }]
            });
        };

The only way to set the JSON API content type was to add it to the defaults:

var api = axios.create({
            headers: {
                'Content-Type': 'application/vnd.api+json'
            }
        });

I was wondering if you know any other way to send the JSON API post with content type application/vnd.api+json beside adding it to the defaults.

Thanks.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
chadmetcalfcommented, Oct 12, 2017

Depending on what you are doing, you need to make sure that the Accept header is set too.

axios.defaults.headers['Content-Type'] = 'application/vnd.api+json';
axios.defaults.headers['Accept'] = 'application/vnd.api+json';
2reactions
theGABScommented, Apr 26, 2017

Axios override default Content-Type axios.defaults.headers.common['Content-Type'] = 'application/vnd.api+json' this doesnt work!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I post JSON to the server? - ReqBin
To send JSON data to the server, you need to provide the JSON data in the HTTP POST request body and pass the...
Read more >
Making a JSON POST Request With HttpURLConnection
In this tutorial, we'll demonstrate how to make a JSON POST request using HttpURLConnection. Further reading: Do a Simple HTTP Request in Java....
Read more >
How to pass json POST data to Web API method as an object?
You need to use JSON.stringify method to convert it to JSON string when you send it,. And the model binder will bind the...
Read more >
Sending json through api post request - Microsoft Q&A
Sending json through api post request · public string AddItem( JObject jsonString) · { · db.Database.ExecuteSqlCommand("exec zotero.
Read more >
JSON:API — Examples
HTTP /1.1 200 OK Content-Type: application/vnd.api+json { "data": [{ "type": ... In the example below, the user is sending an invalid JSON:API request, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found