Translating curl command to Meteor's HTTP call - meteor

I try to "translate" this curl command
curl --request POST --header "Content-Type: application/json" --url http://some-url --user userName:apiKey --data '{ "some": "JSON data as string" }'
into Meteor's HTTP call. I tried this:
const options = {
{ "some": "JSON data as object" },
headers: {
'Content-Type': 'application/json',
},
params: {
user: 'userName:apiKey',
},
// Or alternatively
//
// user: 'userName:apiKey',
};
HTTP.call('POST', 'http://some-url', options, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
With curl command it works fine, with HTTP.call I get a 403, Forbidden. Authorization with userName:apiKey seems to fail. How do I specify the userName:apiKey in the HTTP.call example? Or maybe their is another problem?

If you need authentication, you need to add the auth parameter. The params parameter will actually set all the containing properties to be part of the POST request body.
const options = {
data: { "some": "JSON data as object" },
headers: {
'Content-Type': 'application/json',
},
auth: 'userName:apiKey'
}
Read: https://docs.meteor.com/api/http.html#HTTP-call

Related

Getting ugcPost details through rest/posts api?

I could not get the ugcPost of a post or comment, the urn looks like: urn:li:ugcPost:7023566176156811264,7023567581345140736
how to use this in the api rest/posts/{ugcPosts urn}
the API returns the error {
"message": "Could not find entity",
"status": 404,
"code": "NOT_FOUND"
}
I have also added the headers
{
Linkedin-Version: 202210,
X-RestLi-Protocol-Version: 2.0.0
}
but it is still returning the same error even though I have encoded the urn, example:
https://api.linkedin.com/rest/posts/urn%3Ali%3AugcPost%3A7023566176156811264?viewContext=Reader
You need to add a bearer token in the header. And viewContext=Reader params is not required as per my knowledge of the documentation. I am sharing a sample example.
var axios = require('axios');
var config = {
method: 'get',
url: 'https://api.linkedin.com/rest/posts/urn%3Ali%3AugcPost%3A7023566176156811264',
headers: {
'X-Restli-Protocol-Version': '2.0.0',
'LinkedIn-Version': '202208',
'Authorization': 'Bearer [your_bearer_token]',
}
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
You can visit this link for more information.

How to upload the image using LinkedIn API without using curl request?

I've used the curl request to upload an image which successfully worked for me.
curl -i --upload-file ~/Desktop/Myimage.jpg -H 'Authorization: Bearer Redacted' "https://api.linkedin.com/mediaUpload/C5522AQHn46pwH96hxQ/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQLKRJOn_yNw6wAAAW2T0DWnRStny4dzsNVJjlF3aN4-H3ZR9Div77kKoQ&app=1983914&sync=0&v=beta&ut=1Dnjy796bpjEY1"
I've tried using the same request with "request" package which got failed and redirected me to the "404 page not found" LinkedIn page.
Here is the code:
const options = {
headers: {
'Authorization': `Bearer ${accessToken}`,
'X-Restli-Protocol-Version': '2.0.0',
'Content-Type': 'multipart/form-data'
},
method: 'put',
url: mediaUploadUrl
}
fs.createReadStream(filePath).pipe(request(options, function(err, httpsResponse, body){
if ( err ) {
console.log('err', err);
response(callback, 400, err);
} else {
console.log(body);
response(callback, 200, { mediaUrn });
}
}));
Documentation page that I've followed: https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/rich-media-shares
#linkedin
The request method is supposed to be POST but you are sending PUT instead, below code should work.
const options = {
headers: {
'Authorization': `Bearer ${accessToken}`,
'X-Restli-Protocol-Version': '2.0.0',
'Content-Type': 'multipart/form-data'
},
method: 'post',
url: mediaUploadUrl
}
fs.createReadStream(filePath).pipe(request(options, function(err, httpsResponse, body) {
if ( err ) {
console.log('err', err);
response(callback, 400, err);
} else {
console.log(body);
response(callback, 200, { mediaUrn });
}
}));

How to convert CURL [Send Grid] to http post in Flutter?

CURL Code run fine to send an Email using sendGrid,
I want to convert this code for Flutter Dart Application (as sendGrid does not have any API)
curl --request POST --url https://api.sendgrid.com/v3/mail/send --header 'Authorization: Bearer
API_KEY' --header 'Content-Type: application/json'
--data '{"personalizations": [{"to": [{"email": "test#gmail.com"}]}],
"from": {"email": "test#hotmail.com"},"subject": "Hello, World!",
"content": [{"type": "text/plain", "value": "Hello World App"}]}'
Please provide a full function
I tried something like this ...but it is not working
Future<Map<String, dynamic>> postRequest() async {
var url = 'https://api.sendgrid.com/v3/mail/send';
var body = json.encode({
"personalizations": [
{
"to": [
{"email": "test#gmail.com"}
]
}
],
"from": {"email": "test#hotmail.com"},
"subject": "Hello, World!",
"content": [
{"type": "text/plain", "value": "sent from flutter "}
]
});
var response = await http.post(
url,
headers: {
'Authorization':
'Bearer API_KEY',
'Content-Type': 'application/json',
},
body: body,
);
// todo - handle non-200 status code, etc
print(json.decode(response.body))
return json.decode(response.body);
}
Could you try this ?
import 'package:http/http.dart' as http;
void main() async {
var headers = {
'Authorization': 'Bearer API_KEY',
'Content-Type': 'application/json',
};
var data = '{"personalizations": [{"to": [{"email": "test#gmail.com"}]}], "from": {"email": "test#hotmail.com"},"subject": "Hello, World!", "content": [{"type": "text/plain", "value": "Hello World App"}]}';
var response = await http.post('https://api.sendgrid.com/v3/mail/send', headers: headers, body: data);
print(response.body);
if (response.statusCode != 200) throw Exception('http.post error: statusCode= ${response.statusCode}');
}

Posting Image to Personal Profile Invalid Request Parameters

Unable to share image content through share endpoint, image asset is uploaded through assets API but my request to share API which is copied directly from the example here https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/share-api?context=linkedin/compliance/context#share-content returns an error, invalid parameters in the request body [/Headers] see below details.
Request Headers:
{Authorization: Bearer ***
X-Restli-Protocol-Version: 2.0.0
}
Request Body
{"content":{"contentEntities":[{"entity":"urn:li:digitalmediaAsset:C5622AQEEn3mmqzCb5w"}],"title":"Great Result","landingPageUrl":"https://google.com.au","shareMediaCategory":"IMAGE"},"distribution":{"linkedInDistributionTarget":{}},"owner":"urn:li:person:zzR_UbXjsG","subject":"Great Result","text":{"text":"Great result, couldn't have gone better #realestate"}}
Scopes:
scope=r_emailaddress w_member_social w_organization_social r_basicprofile rw_company_admin rw_organization_admin
Error:
{"serviceErrorCode":100,"message":"Unpermitted fields present in REQUEST_BODY: Data Processing Exception while processing fields [/Headers]","status":403}
It looks like the error message has to do with the headers. Your request body is JSON, but you don't have a Content-Type header set, so this could be the problem:
Content-Type: application/json
Generally, you need a Content-Length header to be sent along with that, but most of the time the client you are using to send the request handles setting that one.
I'm not sure how you're making the request, but here is a fetch() example in JavaScript (make sure you put the correct auth token in the Authorization header):
const url = 'https://api.linkedin.com/v2/shares';
const requestBody = {
"content": {
"contentEntities": [
{
"entity": "urn:li:digitalmediaAsset:C5622AQEEn3mmqzCb5w"
}
],
"title": "Great Result",
"landingPageUrl": "https://google.com.au",
"shareMediaCategory": "IMAGE"
},
"distribution": {
"linkedInDistributionTarget": {}
},
"owner": "urn:li:person:zzR_UbXjsG",
"subject": "Great Result",
"text": {
"text": "Great result, couldn't have gone better #realestate"
}
};
async function makeRequest(url, requestBody) {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ***',
'X-Restli-Protocol-Version': '2.0.0'
},
body: JSON.stringify(requestBody) // body data type must match "Content-Type" header
});
return await response.json(); // parses JSON response into native JavaScript objects
}
// make the actual request
makeRequest(url, requestBody);

How to specify constraint in Meteor Http call?

I need to pass a where constraint(where UserName = "User1") in Meteor http call for Parse Rest APIs. Currently, the result that I get after the below API call includes all the entries not just those where UserName is User1.
var authUrl = "https://api.parse.com/1/classes/ImageData";
Meteor.http.call("GET", authUrl, {
headers: {
"X-Parse-Application-Id": "2CMX1b4JY5xCOPrYEbSc69ucNDDh9pl5yFeqv3A3",
"X-Parse-REST-API-Key": "9UWpw6H7UuBaOEQgT7R3ANQ3rE67yxZxcMHJJaBu",
"content-type": "application/json"
},
params: {
"UserName": "User1",
}
}, function(error, result) {
console.log(JSON.parse(result.content));
}
);
The parse documentation for such an HTTP call in curl representation is:
curl -X GET \
-H "X-Parse-Application-Id: 2CMX1b4JY5xCOPrYEbSc69ucNDDh9pl5yFeqv3A3" \
-H "X-Parse-REST-API-Key: 9UWpw6H7UuBaOEQgT7R3ANQ3rE67yxZxcMHJJaBu" \
-G \
--data-urlencode 'where={"UserName":"User1"}' \
https://api.parse.com/1/classes/ImageData
How can I correctly write this in Meteor?
This seems like it works:
var util = Npm.require('util');
var url = 'https://api.parse.com/1/classes/ImageData';
var result = HTTP.get(url, {
headers: {
'X-Parse-Application-Id': '2CMX1b4JY5xCOPrYEbSc69ucNDDh9pl5yFeqv3A3',
'X-Parse-REST-API-Key': '9UWpw6H7UuBaOEQgT7R3ANQ3rE67yxZxcMHJJaBu',
'content-type': 'application/json'
},
query: 'where={"UserName":"User1"}'
});
console.log(util.inspect(result.data, {depth: null}));
Notes
Meteor.http.call is deprecated. Use the HTTP API instead. Note you will need to $ meteor add http.
Because you need a string and not a key/value pair, use query instead of params. For a GET, both are placed into the query string but your original code made the query ?Username=User1 rather than ?where....

Resources