Body of a request is empty [duplicate] - node.js

I have a React application where I am changing POST method to GET with the request body as it is. It works fine with POST request however when I change the method to GET, it gives me error-
message: "org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public
My Front End Code-
export const setData = (getData) => dispatch => {
axios({
method: 'GET',
url: 'http://localhost:8080/api',
headers: {
'Content-Type': 'application/json'
},
data: getData
})
.then (response => {
dispatch({
type: API_DATA,
payload: response.data
})
dispatch({
type: SET_SEARCH_LOADER,
payload: false
})
})
.catch(function(error) {
})
}
Can someone let me know what I am missing here. As per my understanding, http allows to have a request body for GET method.

As per my understanding, http allows to have a request body for GET method.
While this is technically true (although it may be more accurate to say that it just doesn't explicitly disallow it), it's a very odd thing to do, and most systems do not expect GET requests to have bodies.
Consequently, plenty of libraries will not handle this.
The documentation for Axois says:
// `data` is the data to be sent as the request body
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
Under the hood, if you run Axios client side in a web browser, it will use XMLHttpRequest. If you look at the specification for that it says:
client . send([body = null])
Initiates the request. The body argument provides the request body, if any, and is ignored if the request method is GET or HEAD.

If you want to send parameters with get request in axios, you should send parameters as params.
If you want to set "Content-type":"application/json" and send params with get request, you should also send an empty data object.
For example:
const AUTH_TOKEN = 'Bearer token'
const config = {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': AUTH_TOKEN,
},
data: {},
params: {
"post_id": 1
}
}
axios.get("http://localhost/api/v1/posts/", config)

This is not axios, the error origniates from the java backend you're talking to. The public field in your request body is missing.
If you just want to send the data as parameters (which would be odd), pass it using params instead of data (as shown here: https://github.com/axios/axios#example).
I personally don't think your API should support GET with a request body (talk to the devs and ask for documentation).

Related

JSON not being parsed for validation when doing a POST request to Fastify

In my routes, I have the following:
const reservationSchema = {
body: {
type: 'object',
required: ['in', 'out', 'guests', 'language', 'roomsSelected'],
properties: {
language: {
type: 'string',
},
// ... several other property validations here
}
}
};
fastify.post(
'/api/reservations/:slug',
{ schema: reservationSchema },
reservationsController.addReservation
);
I send the POST request from React like this:
const response = await fetch(process.env.REACT_APP_API_HOSTNAME + '/api/reservations/' + property.slug, {
method: 'POST',
body: JSON.stringify(requestBody)
});
I can see that it is correctly sending JSON when I look at the request:
However I receive the following response:
{
"statusCode":400,
"error":"Bad Request",
"message":"body should be object"
}
Am I missing something to automatically parse the POST body as an object in Fastify so I can validate it using a validation schema? Even in my reservationsController.addReservation() function I need to manually do JSON.parse() on req.body.
From the fetch() docs:
Both request and response (and by extension the fetch() function), will try to intelligently determine the content type. A request will also automatically set a Content-Type header if none is set in the dictionary.
However, (at least in Chrome), when you send a JSON string, it does not intelligently determine that this string is JSON, and instead sends the Content-Type header as text/plain;charset=UTF-8. Since the server receives this Content-Type header, it assumes that you are sending a plan text string and therefore does not parse it as JSON.
To make the server automatically parse the body as JSON, you need to make sure to set the Content-Type header to application/json. Like this:
const response = await fetch(process.env.REACT_APP_API_HOSTNAME + '/api/reservations/' + property.slug, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody)
});

POST request working on Postman but not on JS

Been trying for a couple of days but can't get this request to work on NodeJS, used several request modules (axios, request, node-fetch) but the returned body is blank, same goes for postman if raw option is not selected for body.
Here is how I need to format the body on postman for the request to work:
And this is my last iteration of the request, using node-fetch:
const rawResponse = await fetch(
urlRenfe,
{
method: 'POST',
headers: {
"Cookie": cookie,
"Referer": "https://venta.renfe.com/vol/infoPuntualidadTrenes.do",
"Accept": "*/*",
},
body: `callCount=1
windowName=
c0-scriptName=infoPuntualidadTrenesSvcAjax
c0-methodName=getInfoPuntualidadTrenes
c0-id=0
c0-e1=string:71500
c0-e2=string:X7090115
c0-param0=Object_Object:{cdgoEstacion:reference:c0-e1, terminal:reference:c0-e2}
batchId=1
instanceId=1
page=%2Fvol%2FinfoPuntualidadTrenes.do
scriptSessionId=6ngaztWVMGx8q6B16ABXAlLU1Om/LwA22Om-u7tpxIlXl`
}
);
const response = await rawResponse.text();
I know 100% the headers, url and method are ok, it has to be the body, either on the library or my configuration for it.
Thanks a lot if you give me a hint on how to solve this.

getting problem performing a post request to an external API

I'm using axios to perform get and post requests to an external api,
i have finally succeed to achieve get request (problem with the ssl certificate, i avoid it by adding this :
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
now i would like to post the api,
to get the request working in postman, i put in
headers content-type : application/json
and in the body : {}
like here
when trying with a google chrome extention, to make it work, i put nothing in the headers but in params, i select customer : application/json and i put inside this {} instead of the default choice which is x-www-form-urlencoded;charset=UTF-8
chrome extention
in my javascript app i tried this
var url = https://10.11.31.100:9440/api/nutanix/v3/images/list;
axios({
method:'post',
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
url,
auth: {
username: '******',
password: '********'
},
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
params: {},
data: {}
})
.then(function (response) {
res.send(JSON.stringify(response.data));
console.log(response);
})
.catch(function (error) {
console.log(error);
});
I get this problem :
TypeError : UTF-8 is not a function
Specifically with regards to the Nutanix v3 REST API - this is probably because the POST request above doesn't have an appropriate JSON payload i.e. the "data" parameter is empty.
When sending Nutanix v3 API POST requests, specifically to "list" entities, you'll need to specify the "kind" of entity being listed. In your example, the JSON payload below will work.
{"kind":"image"}
See here: https://nutanix.dev/reference/prism_central/v3/api/images/postimageslist
HTH. :)

Request with request-promise and multipart/form-data

I have to implement the following workflow:
Make request no.1, get a JSON response. Manipulate this JSON object so that it can be used as payload in the second request. Make second request.
The first request is successfully implemented. I get the correct JSON object. The manipulation part is also done and tested.
I managed to create a correct postman collection for the second request. It uses the method POST, has several headers, Content-type is multipart/form-data and the payload from request no.1 is sent as Raw (in the body, options none, form-data etc ... I choose raw).
I am trying to implement this second request but get an error no matter what I try to tweak. The following is my code:
const manipulatedObject = await this._flattenPayload(payload);
const Request = require(request-promise);
const options = {
method: 'POST',
uri: 'https://second_request_url',
formData: {
file: {
value: manipulatedObject,
},
},
headers: {
Accept: 'application/json, text/plain, */*',
Connection: 'keep-alive',
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryxxxxxxxxxxxxxxxx', // this is generated dynamically and matches the value in the manipulatedObject
cookie: this.cachedCookie,
'X-XSRF-TOKEN': this.cachedToken,
},
};
Request(options)
.then((body) => {
return body;
})
.catch((error) => {
return error;
});
* The parameter payload is the response from the first request.
The error I get is this:
message=source.on is not a function, stack=TypeError: source.on is not
a function
The variable manipulatedObject is a string. Currently I am copying it's value from the successful postman request to avoid errors from the manipulation process. The random token in the Content-Type header matches the ones in the manipulatedObject string. So the data are identical to the ones I use in the postman request.
I suspect the error is in the way I send the data, but I am having trouble figuring it out. I have read about this error and it seems to be generated when an object is passed to formData, but I am sending a string so I don't understand why it happens.
The values of formData accepts only three types of elements viz. string, Buffer and Stream. Refer to:request/issues/2366
U may change formData as follows:
formData: {
file: manipulatedObject,
},
or
formData: {
file: {
value: manipulatedObject,
options: {},
},
},

What is the best way to add a "x-api-key" header to a get request in NodeJS + Express?

I want to send a get request but I need to insert my api key in to a 'x-api-key' header. I am using NodeJS + Express. Right now i'm using fetch from the "isomorphic unfetch" lib:
https://github.com/developit/unfetch/tree/master/packages/isomorphic-unfetch
I use it in order to fetch data from the get request. I use this library specially because it works well on both the server and a client.
How should I add the header to my request? Thanks!
There's an example in the unfetch repository that shows how to add headers to fetch requests.
// complex POST request with JSON, headers:
fetch('/bear', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'Bearer XYZ'
},
body: JSON.stringify({ hungry: true })
}).then( r => {
open(r.headers.get('location'));
return r.json();
})

Resources