JEB JEB - 3 months ago 56
React JSX Question

How to make a POST request with json content type with Semantic UI API

I am using the API feature of dropdowns with Semantic UI and want to do a POST request on my API to search for users

By default the API feature of Semantic UI sends form-data content type.

To force the right content-type, I did something like :

receiverDropdown
.dropdown({
apiSettings: {
url: '/api/search/',
method: 'POST',
beforeXHR: (xhr) => {
xhr.setRequestHeader('Content-Type', 'application/json');
},
beforeSend: (settings) => {
settings.data = {
type: 'user',
query: 'test'
}
return settings
}
}
})


but data is still sent in a form-data fashion, like
type=user&query=test
instead of a real JSON object payload like
{type: 'user', query: 'test'}


How can I send a json object with Semantic UI API to my own POST API route ?

Answer

Use JSON.stringify:

JSON.stringify({type: 'user', query: 'test'})
Comments