Nock how to mock put request - put

My real codes like:
fetch(hostname+ '/api/save', {
method: "put",
credentials: "include",
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({a: 'b'})
})
nock like:
nock(hostname)
.put('/api/save')
.reply(200);
the hostname includes port, but it's random port in the test. I've checked the log, the real url and test url are same. But I don't know why it doesn't match???

Related

"Only absolute URLs are supported" When fetch webhooks Nextjs

Im trying to fetch an webhooks url using Environment Variables.
here is my code
const url = process.env.WEBHOOK_URL;
const response = await fetch(
`${url}` ,
{
body: JSON.stringify({
name,
email,
message,
}),
headers: {
'Content-Type': 'application/json'
},
method: 'POST'
}
);
But the error show up "Only absolute URLs are supported"
please help! Thank you

Tampermonkey GM_xmlhttpRequest not sending Request properly

I am trying to implement a tampermonkey script that triggers an API call to a Jira instance to create a ticket with some information found in the page I am on (on a different domain).
Here's what I've tried:
async function createJiraTicket() {
let elements = await obtainThingsForCreatingJiraTicket();
let createJiraTicketUrl = `https://${jiraDomain}/rest/api/latest/issue`;
let requestDataForCreation =`
{
"fields": {
"project": { "key": "${elements.project}" },
"summary": "${elements.title}",
"description": "nothing",
"issuetype": {"name": "Issue" }
}
}`;
GM_xmlhttpRequest ( {
method: "POST",
url: `${http}://${createJiraTicketUrl}`,
user: 'user:pwd', // also tried user:'user', password:'pwd',
data: requestDataForCreation,
header: 'Accept: application/json',
dataType: 'json',
contentType: 'application/json',
onload: function (response) {
jiraTicketLog(`[Ajax] Response from Ajax call Received: `);
}
} );
However, when I run createJiraTicket(), I am getting the following error:
Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at <anonymous>:1:7
I have established the correct #connect tags on the script, so I am pretty blind on where the problem may be...
Can someone help?
Thanks
So I came up with the answer to fix it, apparently it was a number of different details:
So,
Authorization had to be included onto the headers, coded on base64 and keyworded "Basic".
User-Agent needs to be overriden on headers, with any dummy string.
overrideMimeType needed to be set to json.
That all made the trick.
This was the working code.
let createJiraTicketUrl = `//${jiraDomain}/rest/api/latest/issue`;
let authentication = btoa('admin:foobar1');
GM.xmlHttpRequest({
method: "POST",
headers: {
'Accept': 'application/json',
"Content-Type": "application/json",
"Authorization": `Basic ${authentication}`,
"User-Agent": "lolol"
},
url: `${http}:${createJiraTicketUrl}`,
data: JSON.stringify(requestDataForCreation),
dataType: 'json',
contentType: 'application/json',
overrideMimeType: 'application/json',
onload: function (response) {
let url = `${http}://${jiraDomain}/browse/${JSON.parse(response.response).key}`;
log(`${JSON.parse(response.response).key} ticket created: ${url}`);
openJiraTicket(url);
}

Zapier API Configuration: send form-data instead of json request body

I am setting up a Zap for our application in Zapier.
However, I've run into some trouble having the Zap pass over the data in the correct format.
By default it appears Zapier passes the data as json request body, but our backend only accepts form-data.
Is it possible to configure the Zap to send over form-data instead?
In the code below, I've tried to send the data as both params and body, but my backend doesn't any of it as form-data:
const options = {
url: '${URL}',
method: 'POST',
headers: {
'Authorization': ${token},
'Content-Type': 'application/json',
'Accept': 'application/json'
},
params: {
'phone': bundle.inputData.phone,
'email': bundle.inputData.email,
'dialog': bundle.inputData.dialog,
'name': bundle.inputData.name
},
body: {
'name': bundle.inputData.name,
'email': bundle.inputData.email,
'phone': bundle.inputData.phone,
'dialog': bundle.inputData.dialog
}
}
return z.request(options)
.then((response) => {
response.throwForStatus();
const results = z.JSON.parse(response.content);
// You can do any parsing you need for results here before returning them
return results;
});
Any input is greatly appreciated!
I fixed it by replacing 'Content-Type': 'application/json' with 'Content-Type': 'application/x-www-form-urlencoded'.

POST to VoilaNorbert API from Rails with HTTParty

I am trying to POST data to the standard VoilaNorbert search API endpoint using the HTTParty Rails gem:
response = HTTParty.post(
'https://api.voilanorbert.com/2018-01-08/search/name',
query: {
'name': 'Elon Musk',
'domain': 'https://www.tesla.com/'
},
headers: {
'Content-Type': 'application/json',
'Authorization': VOILANORBERT_API_TOKEN
}
)
However, when I try this simple request, I get the response:
=> {"name"=>["This field is required."]}
My understanding is that the query block above is supposed to pass along the name field, which in this case is "Elon Musk".
What am I missing here?
After some trial and error, I succeeded by realizing I needed to change the payload to send form-data in the body of the request rather than as query params. This requires using the body block to encode form fields, as well as the multipart method on the request indicating that data is being uploaded.
response = HTTParty.post(
'https://api.voilanorbert.com/2018-01-08/search/name',
body: {
name: 'Elon',
domain: 'https://www.tesla.com/'
},
multipart: true,
headers: {
'Content-Type': 'application/json',
'Authorization': VOILANORBERT_API_TOKEN
}
)

Parsing params that includes json string in rails controller

In a react-native app, I have the following post request that goes to a rails controller
fetch(POST_PAID_UP, {
method: 'POST',
body: JSON.stringify({
receipt_num: 127,
}).replace(/{|}/gi, "")
})
In the appropriate action of my rails controller I can examine params it looks like this;
{"\"receipt_num\":127"=>nil, "controller"=>"ios/accounts", "action"=>"create"}
so the data is being received. From params I need to extract the value of 'receipt_num'. I have tried JSON.parse(params), but I receive this error;
TypeError: no implicit conversion of Array into String
So how should I parse this data?
You need to add headers that indicate that the information being sent is of type json. So add the following hash to the fetch instruction;
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
You can then remove the .replace(/{|}/gi, "") which is just a work around to overcome the fact that you were not specifying the data was json. The final code should look like this:
fetch(POST_PAID_UP, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
receipt_num: transactionIdentifier,
})
})
You can find more information about fetch here

Resources