I'm trying to create a webhook through the Trello API by using Meteor's HTTP.post method like this:
HTTP.post('https://api.trello.com/1/webhooks?key=...&token=...', {
params: {
idModel: '...',
callbackURL: '...'
},
}, function(error, result) {...});
The request works but the response i get is "Invalid value for idModel". However, if i try the same request using jQuery:
$.ajax({
type: 'POST',
url: https://api.trello.com/1/webhooks?key=...&token=...,
data: {
idModel: '...',
callbackURL: '...'
},
});
Everything works fine (i.e. the webhook is created and data is returned). Somehow Meteor's method seems to make it impossible for Trello to parse the idModel field. Any ideas what might be behind this? Am i doing something wrong or is there a bug?
I solved it by setting the Content-Type header to application/x-www-form-urlencoded. It was sent as text/plain before.
HTTP.post('https://api.trello.com/1/webhooks?key=...&token=...', {
params: {
idModel: '...',
callbackURL: '...'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}, function(error, result) {...});
Try using data instead of params:
HTTP.post('https://api.trello.com/1/webhooks?key=...&token=...', {
data: {
idModel: '...',
callbackURL: '...'
},
}, function(error, result) {...});
Supplementing this page because I had the same problem doing this in golang. I solve it by adding
req.Header.Add("Content-Type", "application/json")
It is confusing that the 400 message is about an invalid idModel.
Related
I am getting mad because as a lodded-in wordpress admin, on the front-end (not in the admin portal), I cannot make post/put requests.
A simple GET AJAX request works perfectly without any type of credentials:
axios.get(this.page.url + "/wp-json/wp/v2/posts").then((resp) => {
console.log(resp.data);
});
BUT, when I try to make post requests I always get 401 error if I do not include the nonce.
If I include the nonce, I get 403. Tried with both AXIOS and JQUERY:
// Axios:
axios.defaults.withCredentials = true;
axios.defaults.headers.post["X-WP-Nonce"] = MYSCRIPT.nonce; // for POST request
axios
.post(this.page.url + "/wp-json/wp/v2/posts", {
title: "title",
content: "content",
status: "publish",
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
// JQUERY
$.ajax({
url: this.page.url + "/wp-json/wp/v2/posts",
type: "post",
data: {
title: "title",
content: "content",
status: "publish",
},
headers: {
"X-WP-Nonce": MYSCRIPT.nonce, //If your header name has spaces or any other char not appropriate
},
dataType: "json",
success: function (data) {
console.info(data);
},
});
The nonce is simply generated with:
<script>
<?php echo 'const MYSCRIPT = ' . json_encode(
array('nonce' => wp_create_nonce('wp-rest'))
); ?>
</script>
I know this is not good practice, and I will include it properly as soon as I get it to work.
The nonce is perfectly retrieved by Javascript, but I get 403 from wordpress...
No idea on how to proceed!
The action specified in wp_create_nonce should be wp_rest (underscore), not wp-rest (hyphen).
https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
For developers making manual Ajax requests, the nonce will need to be passed with each request. The API uses nonces with the action set to wp_rest
Replace wp-rest with wp_rest and it should work properly.
Has anyone worked with Redux? I'm running the same query with Redux and with fetch, but getting different results.
Basically I need to download the file.
Redux:
Query config:
downloadReport: builder.mutation({
query: (body) => ({
url: paths.downloadReport,
method: "POST",
body: body,
}),
}),
return underfined
Fetch:
Fetch config:
return fetch(`/api/reports/download-report`, {
headers: new Headers({'content-type': 'application/json'}),
method: "POST",
body: JSON.stringify(selectedObj),
}).then(downloadFile)};
return:
response: Response
body: ReadableStream
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:3000/api/reports/download-report"
How can I make Redux work like fetch ?
I want to post multiple logs to DataDog from a JS function, using a single HTTP request. Looking at the v2 docs for DataDog's 'send logs' POST endpoint, it sounds like this is possible:
For a single log request, the API ... For a multi-logs request, the API ...
But it's not clear to me from the docs how to actually send a 'multi-logs' request. I've tried the following:
const dataDogEndpoint = 'https://http-intake.logs.datadoghq.com/api/v2/logs';
const body = {
ddtags: 'env:production,status:info',
hostname: 'my-host',
message: ['My first production log.', 'My second production log.'],
service: 'my-service'
};
const response = await fetch(dataDogEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'DD-API-KEY': apiKey
},
body: JSON.stringify(body)
});
Perhaps unsurprisingly, this appears in DataDog as a single log with the following content:
[My first production log., My second production log.]
How can I achieve this?
Got it - this can be achieved by adding multiple log objects to the body like so:
const dataDogEndpoint = 'https://http-intake.logs.datadoghq.com/api/v2/logs';
const body = [{
ddtags: 'env:production,status:info',
hostname: 'my-host',
message: 'My first production log.',
service: 'my-service'
},{
ddtags: 'env:production,status:info',
hostname: 'my-host',
message: 'My second production log.',
service: 'my-service'
}];
const response = await fetch(dataDogEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'DD-API-KEY': apiKey
},
body: JSON.stringify(body)
});
(You'll probably want a loop instead of instantiating each log object separately.)
I have a route in Rails API '/api/pay'. My client side post request successfully hits the controller action, however nothing I send in the body: JSON.stringify('...') gets through to the back-end. Other post requests I have made work just fine with the same format.
export const payForItem = (payData) => {
return dispatch => {
dispatch(payForItemStart());
// ?userID=${data.userID}&adID=${data.adID}&price=${data.price}
const data = {userID: payData.userID, adID: payData.adID, price: payData.price}
fetch(`/api/pay`, {
method: 'POST',
header: {
'content-type': 'application/json'
},
body: JSON.stringify(data)
})
Here is what payData looks like.
Rails Api back-end params
Probably you've got typo in headers section. Should be plural headerS with s:
headers: {
"Content-Type": "application/json"
}
I've tried with these Meteor HTTP.get methods in the server side:
HTTP.get(`http://graph.facebook.com/${fbUserId}/picture?callback=?`, {
headers: {
'content-type': 'application/json',
}
}, function(data){
console.log(data); // [Error: failed [400]]
});
HTTP.get(`https://graph.facebook.com/${fbUserId}/picture`, {
headers: {
'content-type': 'application/json',
}
}, function(data){
console.log(data); // null
});
However, this works in the client side with jQuery:
$.getJSON(`http://graph.facebook.com/${fbUserId}/picture?callback=?`, function(data){
console.log(data); // {url: 'http://img-url...'}
});
Any ideas?
UPDATE
#Ethaan answer worked (with a few additions):
function _fetchUserFacebookThumbnail(fbId){
return new Promise(function(resolve, reject){
HTTP.get(`https://graph.facebook.com/${fbId}/picture/?redirect=0&type=square`, function(error, data){
resolve(data['data']);
});
});
}
According to FB Developers docs, the ?redirect=0 query requests a JSON object instead of the image itself.
You are getting null on the second one because there is not error, so that means the second example work, try with error, data instead of just error.
Also if you only want to get the picuture you can just link to this.
facebookPicture = "https://graph.facebook.com/" + userFacebookId + "/picture/?type=large";
btw, mexican developer over here head ups =p.