Google apps script: UrlFetchApp does not support body inside a request - api

trying to make a small script that would update stock in my store. This code should be working with yandex market api. They do not accept payload parameter and waiting for 'body' in the request. But at the same time look like URLFetchApp (google apps script) only can send payload and body isn't supported. Is there any way to resolve this?
function api_request(){
const url = 'https://api.partner.market.yandex.ru/v2/campaigns/2252398/offers/stocks.json'
var body = {"skus": [{"sku": "1", "warehouseId": 355055, "items": [{"type": "FIT", "count": 15, "updatedAt": get_utc_date()}]}]}
var body = JSON.stringify(body)
const params = {
method: 'Put',
'Content-Type': 'application/json',
muteHttpExceptions: true,
//'payload': body,
'body': body,
headers: {
'Authorization': 'OAuth oauth_token="abcd", oauth_client_id="abc"'
}
};
console.log(params)
var r = UrlFetchApp.fetch(url, params);
r = JSON.parse(r);
console.log(r)
}
Sending this as request:
{ method: 'Put',
'Content-Type': 'application/json',
muteHttpExceptions: true,
body: '{"skus":[{"sku":"1","warehouseId":355055,"items":[{"type":"FIT","count":15,"updatedAt":"2022-12-27T20:28:10+03:00"}]}]}',
headers: { Authorization: 'OAuth oauth_token="abcd", oauth_client_id="abc"' } }
And getting this response:
{ error: { code: 400, message: 'Required request body is missing' },
errors:
[ { code: 'BAD_REQUEST',
message: 'Required request body is missing' } ],
status: 'ERROR' }
When payload used - getting the same response.
Any ideas please? :)
I've tried to use body / payload, getting the same response

Related

How to get response with axios api with "GET" method in react native

Here is my code:
axios({
method: "GET",
url: "http://112.196.108.244:9002/api/survey/question/get-question/not-answered/?surveyId=",
headers: {
"content-type": "application/json",
Authorization: `Bearer token-key`,
},
body: {
id: "68367859",
isMandatory: "false",
paginationFilter: { limit: 10, offset: 0, order: "DESC" },
filterInput: {
locationIds: ["1", "4011403", "4012144"],
categoryIds: [
"twoSubCategories/7898496",
"domains/7895290",
"subCategories/7896491",
],
},
},
})
.then((response) => {
console.log("response", response);
})
.catch((error) => {
console.log("error", error.response.data);
});
this code gives me error:
The error in console is-
details: "uri=/api/survey/question/get-question/not-answered/"
message: "document key is not valid."
status: 400
You're passing the id in the body. There are two problems at play here:
GET requests shouldn't use a body as part of the request. Check this answer.
What you want to do is pass the id (Which I assume is the survey id) as a query parameter. Something like this should work:
axios({
method: 'GET',
url: 'http://112.196.108.244:9002/api/survey/question/get-question/not-answered/',
headers: {
'content-type': 'application/json',
Authorization: "Bearer token-key"
},
params: {
surveyId: "68367859"
}
})
Add other params as necessary.

Cypress send image in request

I am trying to send image as a part of POST request, any idea how i can do this?
Request:
cy.request({
method: 'post',
url: '/some/url',
body: {
'name': data.Name,
'slug': data.Slug,
'new_preview_image': 'images/test.jpeg',
},
headers: {
accept: 'application/json'
},
auth: {
'bearer': ''
}
});
That is the request i am trying to send, but for image i am getting response
Body: {
"message": "The new preview image must be an image."}

How to handle the plain/text POST request in the cypress

I have a postman collection and It's POST call and the request body is type of plain/text and I just want to automate this using cy.request but I'm not sure how to pass the test body in the cy.request body section and it returned 400 bad request if I run the below code.
cy.request({
url: `${url}/user`,
method: "POST",
headers: {
'Content-Type': 'plain/text'
},
body: {
"confirmEmail": "true"
}
}).then(res =>{
cy.task('log',"Email id "+res.body.emailAddress);
return res.body;
});
}
The above request return .json response but the input request if text format and the same working fine in the postman tool.
Passing the request body in the below format in the postman tool and its working fine.
confirmEmail=true
My assumption is in the request body our endpoint is expecting a boolean value, but you are passing a string. So changing "confirmEmail": "true" to "confirmEmail": true should work.
cy.request({
url: `${url}/user`,
method: 'POST',
headers: {
'Content-Type': 'plain/text',
},
body: {
confirmEmail: true,
},
}).then((res) => {
cy.log(res.body.emailAddress) //prints email address from response body
})
In case you need to pass parameters in your URL you can directly use qs
cy.request({
url: `${url}/user`,
method: 'POST',
qs: {
confirmEmail: true,
},
headers: {
'Content-Type': 'plain/text',
},
}).then((res) => {
cy.log(res.body.emailAddress) //prints email address from response body
})

my react-native app fail to send body in POST request to backend url

As i am trying to send my data in form of body in backed url as in backed i have made something if it dont receive body it will send sucess: false, msg: haven't received body else sucess: true, msg: jwt token as if i make request from post man with same data it's working but sending via. native app it fails to send.. any help will be helpfull
As 1st request is from postman and 2nd from my app
const handleLogin = (Enrno, Pass) => {
setError(null);
setIsLoaded(false);
setItems([]);
fetch(config.url + "/login", {
method: "POST",
header : {
Accept : 'application/json',
'Content-Type' : 'application/json'
},
body : JSON.stringify({
"enrno": Enrno,
"password" : Pass
})
})
.then((res) => res.json())
.then(
(result) => {
setIsLoaded(true);
setItems(result);
alert(items[0].msg);
},
(error) => {
setIsLoaded(true);
setError(error);
}
);
};
I think you need to put these to headers, not header.
Accept : 'application/json',
'Content-Type' : 'application/json'
So, it should look like this.
fetch(config.url + "/login", {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
enrno: Enrno,
password : Pass
})
})

Getting false response and empty data array in react-native post save data to server

I am trying to save a simple registration form to server using fetch but getting response false.
apiDataSave = () => {
fetch('<myapi>', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
name : "abc",
no : "1234567890",
password : "123",
email : "abc#gmail.com"
})
})
.then(function(response) {
return response.json();
})
.then(function(data) {
alert(`response json is: ${JSON.stringify(data)}`)
})
}
I am getting alert as follows:
response json is: {"status":false,"data":[]}
The expected result alert is:
response json is: {"status":true,"data":"1"} // number of records entered in database
The problem was that the Content-Type was not 'application/json' but following:
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
I got the answer here:
stackoverflow answer