I'm trying to convert this cURL command into a request with AXIOS
`curl -X POST "https://serverless-upload.twilio.com/v1/Services/${service_uid}/Functions/${function_uid}/Versions" \
-F "Content=#./template_scripts/collect.js; type=application/javascript" \
-F "Path=collect.js" \
-F "Visibility=public" \
-u "${client.accountSid}:${client.password}"`
my attempt at doing so looks like this:
collect_file = ""
await axios.get("https://pastebin.com/raw/RFYs4n2p").then((r) => collect_file = r.data)
url = `https://serverless-upload.twilio.com/v1/Services/${service_uid}/Functions/${function_uid}/Versions`
form = new FormData();
form.append("Path", "collect");
form.append("Visibility", "public");
form.append("Content", collect_file);
form.append("filename", "collect.js");
form.append("contentType", "application/javascript");
await axios.post(url, form, {
headers: {
Authorization: 'Basic ' + Buffer.from(`${accountSid}:${authToken}`).toString('base64'),
...form.getHeaders(),
},
})
When filling the "Content" param I'm using the text representation of a JS file that I have hosted on Pastebin. In the initial cURL command you see I specify the file ./template_scripts/collect.js I don't want to use fs to load the "collect.js" file as I want the code to not use the file system.
The error I get is
Error: Request failed with status code 400
{
message: 'Invalid function version content.',
code: 20001,
user_error: true,
http_status_code: 400,
params: {}
}
400
{
date: 'Wed, 10 Feb 2021 13:39:08 GMT',
'content-type': 'application/json',
'content-length': '113',
connection: 'close',
't-request-id': 'RQ1bf727ce267b5b974cd1eeab122ad02e',
'x-shenanigans': 'none',
'access-control-allow-origin': '*',
'access-control-allow-headers': 'Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since',
'access-control-allow-methods': 'GET, POST, DELETE, OPTIONS',
'access-control-expose-headers': 'ETag',
'access-control-allow-credentials': 'true',
'x-powered-by': 'AT-5000',
'x-home-region': 'us1',
'x-api-domain': 'serverless-upload.twilio.com',
'strict-transport-security': 'max-age=31536000'
}
I'm assuming the problem may be with using the text version of the JS file but not entirely sure. When read from pastebin it is returned as type "string".
Thank You!
Place the filename as the third argument for "Content" param
form.append("Content", collect_file, "collect.js");
Related
I'm trying to run an XML post using node-fetch but I get this error:
org.xml.sax.SAXException: No deserializer for {http://www.w3.org/2001/XMLSchema}anyType
Here is the body I send:
<soapenv:Body>
<e:getXMLListObject>
<table>Enterprise</table>
<fields>
<item>EntCorpName</item>
<item>EntAcronym</item>
<item>EntSiren</item>
<item>EntSiret</item>
<item>EntAd1</item>
<item>EntZip</item>
<item>EntCity</item>
</fields>
<query>
<item>
<item>Entid = '001152000000'</item>
</item>
</query>
</e:getXMLListObject>\n
</soapenv:Body>
An idea please, this is the first time I use this type of body (table) for the others, it works very well.
The code: await fetch(url,options).then(response => {...}
I put this xml in the body parameter in options with soapenv:Envelope ofc.
let options = {
method: 'POST',
body: xml,
timeout: 10000,
credentials: 'include',
headers: {
'Content-Type': 'text/xml;application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length': xml.length,
'Accept': 'text/xml',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Origin': true,
'soapaction': 'add',
"Access-Control-Expose-Headers":"Authorization",
"Cookie": cookies
}
};
NB : Is a client Xml.
I tried several approach to match this specific pattern of the url:
https://app.launchdarkly.com/sdk/goals/123123123
so the 123123123 will be always changing.also for some reason its making a OPTION call beside another GET call every time. Not sure why and that's likely another story...
nock return error like:
Error: Error: Nock: No match for request {
"method": "OPTIONS",
"url": "https://app.launchdarkly.com/sdk/goals/123123123",
"headers": {
"origin": "http://localhost",
"access-control-request-method": "GET",
"access-control-request-headers": "X-LaunchDarkly-User-Agent",
"user-agent": "Mozilla/5.0 (darwin) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/16.5.3",
"host": "app.launchdarkly.com",
"content-length": 0
}
}
nock is not recognizing the pattern if I do (note that I am copying the same pattern as a GET as well)
nock('https://app.launchdarkly.com')
.persist()
.defaultReplyHeaders({
'access-control-allow-origin': '*',
'access-control-allow-headers': '*',
'access-control-allow-credentials': 'true',
})
.options('/sdk/goals.*$/')
.reply(200, mockLDExperiments);
or
nock('https://app.launchdarkly.com')
.persist()
.defaultReplyHeaders({
'access-control-allow-origin': '*',
'access-control-allow-headers': '*',
'access-control-allow-credentials': 'true',
})
.options('/sdk/goals/**/*')
.reply(200, mockLDExperiments);
or
nock('https://app.launchdarkly.com')
.persist()
.defaultReplyHeaders({
'access-control-allow-origin': '*',
'access-control-allow-headers': '*',
'access-control-allow-credentials': 'true',
})
.options('/sdk/goals')
.reply(200, mockLDExperiments);
any idea how to write the correct path matcher so I can allow this segment scenario gets picked up by nock?
Nock supports Regex path matching. It seems you're attempting something similar with globs, however, if a string is provided Nock only does exact matching.
Docs
For your case, something like this should get you going.
nock('https://app.launchdarkly.com')
...
.options(/^\/sdk\/goals\//)
...
I'm using Postman and trying to send a POST request to an API with an image, one of the required parameters to do so is a file "Base 64 encoded file content string. Maximum file size to upload 5Mb". Not sure what else I can try.
the example they give in the documentation is:
"file": "https://example.com/image.png"
*from:
curl -i -u <api_username>:<api_password> -H "Content-type: application/json" -H "Security-Key: <your_private_key>" -XPOST https://api2.questblue.com/fax/send -d "{"did_from": 3332224444, "did_to": 1112223333, "file": "https://example.com/image.png"}"
I tried uploading a test image to a image hosting site then included its link that but got:
"error": "File Type is not Supported"
(My test image was also a png)
filetypes accepted are:(string)jpeg, jpg, gif, png, tif, tiff, pdf, doc, rtf, ods, xls, csv, ppt, txt, rar, zip, 7z4
I've also tried running my test image through https://www.base64decode.org/ then used the string it output but received an error saying URI was to long.
*I also just tried uploading the file, got the same File Type is not Supported error:
var fs = require('fs');
var options = {
'method': 'POST',
'url': 'https://api2.questblue.com/fax/send',
'headers': {
'Content-type': 'application/json',
'Security-Key': 'key',
'Authorization': 'auth'
},
formData: {
'did_from': 3332224444,
'did_to': 1112223333,
'filename': 'testfax',
'file': {
'value': fs.createReadStream('C:/Users/zee/folder/testfax.jpg'),
'options': {
'filename': 'C:/Users/zee/folder/testfax.jpg',
'contentType': null
}
}
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});```
I am trying to access https://grocerybear.com/#docs sample api request which is shown in the documentation as
curl -H "api-key: 123ABC" \
-H "Content-Type: application/json" \
-X POST \
-d '{"city":"LA", "product":"bread", "num_days": 10}' \
https://grocerybear.com/getitems
I used a website online to convert this into a javascript fetch function and got this:
fetch("https://grocerybear.com/getitems", {
body: "{\"city\":\"LA\", \"product\":\"bread\", \"num_days\": 10}",
headers: {
"Api-Key": "123ABC",
"Content-Type": "application/json"
},
method: "POST"
})
I tested this with javascript with my developer key and it worked, however I want to use the http methods in Angular 9 to get the data.
I tried this:
getData(){
const options = {
headers: new HttpHeaders(
{
"Api-Key": '(myDeveloper key was inserted here in the program)' as const,
"Content-Type": 'application/json' as const
}
),
body: "{\"city\":\"LA\", \"product\":\"bread\", \"num_days\": 10}"
};
return this.http.get(`https://grocerybear.com/getitems`, options);
}
and it returned a 400 error when I tried to log it to the page.
When I changed it to a post method request instead of a get method then it also returns a 400 error.
Does anybody know how to implement this to get the proper data back?
Thanks!
I just figured it out!
the correct code would've been:
getData(){
const body = {city:"LA", product:"bread", num_days: 10}
const options = {
headers: new HttpHeaders(
{
"Api-Key": 'API KEY inserted here' as const,
"Content-Type": 'application/json' as const
}
)
};
return this.http.post('https://grocerybear.com/getitems', body, options);
}
}
I'm trying to authenticate on Enterprise GitHub with #octokit/rest. If I use the following Curl I get a list of the API's URL's:
curl -u "my#email.com" https://api.github.<my domain>.com
when I run that I am prompted for a password where I enter my personal access token.
However, If I use the same details in my node application I get a 401 Bad Credentials response. This is how I'm configuring my authentication:
octokit.authenticate({
baseUrl: 'https://api.github.<my domain>.com',
type: 'basic',
username: 'my#email.com',
password: '<my personal access token>'
});
Based on the documentation I believe this should work. can anybody advise why it doesn't?
This is the full HTTP response I get:
{ HttpError: {"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}
at response.text.then.message (/node_modules/#octokit/rest/lib/request/request.js:72:19)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
name: 'HttpError',
code: 401,
status: undefined,
headers:
{ 'access-control-allow-origin': '*',
'access-control-expose-headers': 'ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval',
connection: 'close',
'content-length': '83',
'content-security-policy': 'default-src \'none\'',
'content-type': 'application/json; charset=utf-8',
date: 'Mon, 10 Sep 2018 13:20:12 GMT',
'referrer-policy': 'origin-when-cross-origin, strict-origin-when-cross-origin',
server: 'GitHub.com',
status: '401 Unauthorized',
'strict-transport-security': 'max-age=31536000; includeSubdomains; preload',
'x-content-type-options': 'nosniff',
'x-frame-options': 'deny',
'x-github-media-type': 'github.v3; format=json',
'x-github-request-id': 'C8BA:76CE:24B2BB3:45F82A2:5B966F8B',
'x-ratelimit-limit': '10',
'x-ratelimit-remaining': '9',
'x-ratelimit-reset': '1536585672',
'x-runtime-rack': '0.039959',
'x-xss-protection': '1; mode=block' } }
Addtional Information
This seems to be where my issue occurs:
let response = await method({
q: "repo:" + repoOrg + "/" + repoName + " is:issue",
per_page: 100
});
This is similar to the example on the npm page. Is it in some way possible the authentication isn't getting appliet to this and if so how do I ensure it does get applied?
In order to authenticate with a token you should set the auth type to token and generate a token
username takes both email and username
Full working example :
const octokit = require('#octokit/rest')();
octokit.authenticate({
type: 'token',
username: 'my username',
token: 'my token'
});
octokit.activity.getStarredRepos().then(results => {
console.log(results);
});
The .authenticate method does not accept a baseUrl parameter. You have to add it to the Octokit constructor, see https://github.com/octokit/rest.js#client-options
Here's an example from my code:
const octokit = require('#octokit/rest')({
baseUrl: 'https://api.github.<my domain>.com'
})
octokit.authenticate({
type: 'basic',
username: 'my#email.com',
password: '<my personal access token>'
});