to generate body contents as --data-raw from httpsnippet - node.js

I am trying to generate raw body from the
const snippet = new HTTPSnippet({
method: 'POST',
url: 'http://mockbin.com/request',
headers: [{
name: 'content-type',
value: 'Application/json',
}],
queryString: [
{
"name": "param1",
"value": "value1",
"comment": ""
},
{
"name": "param1",
"value": "value1",
"comment": ""
}
],
postData: {
"mimeType": "application/x-www-form-urlencoded",
"params": [{
"name": "body1",
"value": "sds",
"comment": ""
}],
"text" : "plain posted data",
"comment": "",
}
});
snippet.convert('shell', 'curl');
but just the value of text plain posted data is being printed in the body (postman).
but i am expecting the body as
{
"body1": "sds"
}
also I noticed it generate curl as --data 'plain posted data' but it should be like
--data-raw '{ "body1": "sds" }'

Related

Jira error when creating issue in node.js

The following code gives me the error "Invalid request payload. Refer to the REST API documentation and try again" when is executed and I dont know where the error is
const bodyData =
`"fields": {
"summary": "Summit 2019 is awesome!",
"issuetype": {
"name": "Task"
},
"project": {
"key": "PRB"
},
"description": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"text": "This is the description.",
"type": "text"
}
]
}
]
}
}`;
fetch('https://mysite.atlassian.net/rest/api/3/issue', {
method: 'POST',
headers: {
'Authorization': `Basic ${Buffer.from('myemail#gmail.com:mytoken').toString('base64')}`,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: bodyData
}).then(response => {
console.log(
`Response: ${response.status} ${response.statusText}`
);
return response.text();
}).then(text => console.log(text)).catch(err => console.error(err));
Problem
Your bodyData is not a valid json
Solution
Wrap it with {}
`{
fields: {
"summary": "Summit 2019 is awesome!",
"issuetype": {
"name": "Task"
},
"project": {
"key": "LOD"
},
"description": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"text": "This is the description.",
"type": "text"
}
]
}
]
}
}
}`
P.S.
You would find this error if you would use JSON.stringify with an object instead of a string.
const bodyData = JSON.stringify(
{
fields: {
"summary": "Summit 2019 is awesome!",
"issuetype": {
"name": "Task"
},
"project": {
"key": "LOD"
},
"description": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"text": "This is the description.",
"type": "text"
}
]
}
]
}
}
});

New Relic: Script always returns Status 400 but works in Postman

I'm trying to make a healthcheck for my service in New Relic. So I just want to call one of my APIs every x minutes and see if it returns 200.
In New Relic I created a new synthetic monitor and now I'm trying to write a script for that monitor.
The script is supposed to make a post request to our service and receive a token in the response with status 200. In Postman this post request works and returns the token + Status 200 (I replaced the sensitive strings with <...>):
curl --location --request POST <TOKEN_URL> \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=<CLIENT_ID_DEV>' \
--data-urlencode 'client_secret=<CLIENT_SECRET_DEV>'
But when I try to recreate that with the script, it always returns Status 400 Bad Request.
This is my script:
var assert = require('assert');
//Defining my authentication credentials.
var IDK_TOKEN_URL = $secure.TOKEN_URL;
var CLIENT_ID = $secure.CLIENT_ID_DEV;
var CLIENT_SECRET = $secure.CLIENT_SECRET_DEV;
var options = {
url: IDK_TOKEN_URL,
body: JSON.stringify({
grant_type: 'client_credentials',
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
}),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
//Define expected results using callback function.
function callback(error, response, body) {
console.log(response.statusCode + " status code")
assert.ok(response.statusCode == 200, 'Expected 200 OK response');
var info = JSON.parse(body);
assert.ok(info.success == true, 'Expected True results in Response Body, result was ' + info.success);
console.log("End reached");
}
//Make POST request, passing in options and callback.
$http.post(options, callback);
This is what I see in the console:
It seems to automatically append a port 443 to my url, so instead of <my url>.io/oidc/v1/token, the request seems to get fired to <my url>.io:443/oidc/v1/token?
And when I click on "View resource" in the image above, I see:
But I'm using a post method, why is it saying anything about get method is not allowed?
This is the HAR log that I can download in the new relic console:
"request": {
"cookies": [],
"headers": [
{
"name": "Content-Type",
"value": "application/x-www-form-urlencoded"
},
{
"name": "host",
"value": "<my url>.io"
},
{
"name": "content-length",
"value": "187"
},
{
"name": "X-Abuse-Info",
"value": "Request sent by a New Relic Synthetics Monitor (https://docs.newrelic.com/docs/synthetics/new-relic-synthetics/administration/identify-synthetics-requests-your-app) - monitor id: df1817f0-fac2-49f4-a0d5-479d254dfa1a | account id: 2807718"
},
{
"name": "X-NewRelic-Synthetics",
"value": "[1,2807718,\"fddc843c-8fe0-497f-bf5b-52c2805a265e\",\"b6da79b9-37ab-4a8a-a792-f3fa0f99f205\",\"df1817f0-fac2-49f4-a0d5-479d254dfa1a\"]"
}
],
"headersSize": 607,
"bodySize": 187,
"method": "POST",
"url": "<my url>.io:443/oidc/v1/token/",
"httpVersion": "HTTP/1.1",
"queryString": [],
"postData": {
"mimeType": "application/x-www-form-urlencoded",
"text": "{\"grant_type\":\"client_credentials\",\"client_id\":\"_SECURECREDENTIAL_\",\"client_secret\":\"_SECURECREDENTIAL_\"}",
"params": []
},
"_ajax": false,
"_mixedContentType": "unknown",
"_referrerPolicy": "",
"_isLinkPreload": false,
"_host": "<my url>.io",
"_port": 443,
"_path": "/oidc/v1/token/"
},
"response": {
"cookies": [],
"headers": [
{
"name": "Date",
"value": "Thu, 06 May 2021 10:21:05 GMT"
},
{
"name": "Content-Type",
"value": "application/json"
},
{
"name": "Content-Length",
"value": "67"
},
{
"name": "Connection",
"value": "close"
},
{
"name": "Cache-Control",
"value": "no-cache, no-store, max-age=0, must-revalidate"
},
{
"name": "Expires",
"value": "0"
},
{
"name": "Pragma",
"value": "no-cache"
},
{
"name": "Referrer-Policy",
"value": "origin"
},
{
"name": "Strict-Transport-Security",
"value": "max-age=31536000 ; includeSubDomains"
},
{
"name": "Vary",
"value": "accept-encoding,origin,access-control-request-headers,access-control-request-method,accept-encoding"
},
{
"name": "X-Content-Type-Options",
"value": "nosniff"
},
{
"name": "X-Frame-Options",
"value": "DENY"
},
{
"name": "X-Vcap-Request-Id",
"value": "e2006a3c-0c27-4194-6b81-d9f037158ca3"
},
{
"name": "X-Xss-Protection",
"value": "1; mode=block"
}
],
"headersSize": 544,
"bodySize": 67,
"status": 400,
"statusText": "Bad Request",
"httpVersion": "HTTP/1.1",
"content": {
"size": 639,
"compression": 572,
"mimeType": "application/json",
"text": ""
},
"redirectURL": "",
"_chromeStatusText": "Bad Request",
"_connectionReused": false,
"_fromServiceWorker": false,
"_fromDiskCache": false,
"_fromAppCache": false,
"_fromCache": false
},
I had to replace 'body' with 'form' like in this example.
I aslo added now the call to the API after the token was received. The final script was:
var assert = require('assert');
//Define your authentication credentials.
var TOKEN_URL = $secure.TOKEN_URL;
var MY_SERVICE_BASE_URL = $secure.MY_SERVICE_BASE_URL_DEV;
var CLIENT_ID = $secure.CLIENT_ID_DEV;
var CLIENT_SECRET = $secure.CLIENT_SECRET_DEV;
function new_relic_callback(err, response, body) {
assert.equal(response.statusCode, 200, 'Expected a 200 OK response');
};
function api_request_callback(err, response, body) {
var parsed_body = JSON.parse(body);
var api_request = {
url: CONSENT_BASE_URL + '/rest of URL...',
headers: {
'Authorization': 'Bearer ' + parsed_body["access_token"]
}
};
$http.get(api_request, new_relic_callback);
};
var token_request = {
url: TOKEN_URL,
form: {
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: "client_credentials"
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
$http.post(token_request, api_request_callback);

How to get valid JSON response from axios library using nodeJS

I'm trying to hit the REST endpoint by using AXIOS library and the response.data return the below in console.log:
Reseponse for console.log(response.data)
{
sqlQuery: "select type,subtype from wetrade_p2 where parent_id='69341269'",
message: '1 rows selected',
row: [ { column: [Array] } ]
}
But when I hit the same REST endpoint in postman and I can get the entire Response JSON as like below:
PostMan Output (Expected):
{
"sqlQuery": "select type,subtype from wetrade_p2 where parent_id='69341269'",
"message": "2 rows selected",
"row": [
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "P",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
},
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "S",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
}
]
}
I also tried to stingify the response.data and it returned below response which is not able to parse()
Getting below response in console.log when I tried to use JSON.stringify(response.data):
sqlQuery: "select type,subtype from wetrade_p2 where parent_id=69341269"
message: "2 rows selected"
row: [
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "P",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
},
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "S",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
}
]
Sample Code:
await axios[methodType](url, body, {
httpsAgent:httpsAgent,
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Access-Control-Allow-Origin": true
}
}).then(response => {
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
console.log(response.data);
console.log(JSON.stringify(response.data))
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
console.log(response);
}).catch(error => {
console.log(error);
});
You DO get the right data, just node.js doesn't display it in the console/stdout. You can use util.inspect() for a better formatted output. Try this:
const util = require('util');
// ...
console.log(util.inspect(response.data, { showHidden: false, depth: null }));

Azure AD B2C REST API : Create Bulk Local Accounts

Is it possible to create users in bulk via the REST API. Same as we do for single user in the below URL.
https://graph.windows.net/{MYADB2C}.onmicrosoft.com/users?api-version=1.6
We have the provision via the Azure portal but could'nt find anything with REST API.
UPDATED
Sample request for Batch processing
POST https://graph.windows.net/{}.onmicrosoft.com/$batch?api-version=1.6
Headers :
Authorization : {token}
Content-Type : multipart/mixed; boundary=changeset_***********
Body :
{
"requests": [
{
"id": "1",
"method": "POST",
"url": "/users",
"body": {
"accountEnabled": true,
"creationType": "LocalAccount",
"displayName": "test1#gamil.com",
"passwordPolicies": "DisablePasswordExpiration, DisableStrongPassword",
"passwordProfile": {
"password": "***",
"forceChangePasswordNextLogin": false
},
"signInNames": [
{
"type": "emailAddress",
"value": "test1#gamil.com"
}
]
},
"headers": {
"Content-Type": "application/json"
}
},
{
"id": "2",
"method": "POST",
"url": "/users",
"body": {
"accountEnabled": true,
"creationType": "LocalAccount",
"displayName": "test2#gmail.com",
"passwordPolicies": "DisablePasswordExpiration, DisableStrongPassword",
"passwordProfile": {
"password": "***",
"forceChangePasswordNextLogin": false
},
"signInNames": [
{
"type": "emailAddress",
"value": "test1#gamil.com"
}
]
},
"headers": {
"Content-Type": "application/json"
}
}
]
}
Yes. You can batch operations by referring to Batch processing | Graph API concepts.
But we recommend you use Microsoft Graph API JSON Batching instead of Azure AD Graph Batch processing because Azure AD Graph content is no longer updated.
An example using Microsoft Graph API here:
POST https://graph.microsoft.com/v1.0/$batch
Accept: application/json
Content-Type: application/json
{
"requests": [
{
"id": "1",
"method": "POST",
"url": "/users",
"body": {
"accountEnabled": true,
"displayName": "at1",
"mailNickname": "at1",
"userPrincipalName": "at1#**.onmicrosoft.com",
"passwordProfile" : {
"forceChangePasswordNextSignIn": true,
"password": "password-value"
}
},
"headers": {
"Content-Type": "application/json"
}
},
{
"id": "2",
"method": "POST",
"url": "/users",
"body": {
"accountEnabled": true,
"displayName": "at2",
"mailNickname": "at2",
"userPrincipalName": "at2#**.onmicrosoft.com",
"passwordProfile" : {
"forceChangePasswordNextSignIn": true,
"password": "password-value"
}
},
"headers": {
"Content-Type": "application/json"
}
},
{
"id": "3",
"method": "POST",
"url": "/users",
"body": {
"accountEnabled": true,
"displayName": "at3",
"mailNickname": "at3",
"userPrincipalName": "at3#**.onmicrosoft.com",
"passwordProfile" : {
"forceChangePasswordNextSignIn": true,
"password": "password-value"
}
},
"headers": {
"Content-Type": "application/json"
}
}
]
}
You can have a quick test in Microsoft Graph Explorer.

API call made using requests in Python 3 returns HTML not JSON

I'm using Python 3.7 and requests 2.21.0 to integrate payment gateway.
I have the following example form sandbox which works as expected (returns JSON):
curl -X POST https://secure.snd.payu.com/api/v2_1/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer d9a4536e-62ba-4f60-8017-6053211d3f47" \
-d '{
"notifyUrl": "https://your.eshop.com/notify",
"customerIp": "127.0.0.1",
"merchantPosId": "300746",
"description": "RTV market",
"currencyCode": "PLN",
"totalAmount": "21000",
"buyer": {
"email": "john.doe#example.com",
"phone": "654111654",
"firstName": "John",
"lastName": "Doe",
"language": "pl"
},
"settings":{
"invoiceDisabled":"true"
},
"products": [
{
"name": "Wireless Mouse for Laptop",
"unitPrice": "15000",
"quantity": "1"
},
{
"name": "HDMI cable",
"unitPrice": "6000",
"quantity": "1"
}
]
}'
On the other hand, I have the following Python code
import requests
import json
url = "https://secure.snd.payu.com/api/v2_1/orders"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer d9a4536e-62ba-4f60-8017-6053211d3f47"
}
data = {
"notifyUrl": "https://your.eshop.com/notify",
"customerIp": "127.0.0.1",
"merchantPosId": "300746",
"description": "RTV market",
"currencyCode": "PLN",
"totalAmount": "21000",
"buyer": {
"email": "john.doe#example.com",
"phone": "654111654",
"firstName": "John",
"lastName": "Doe",
"language": "pl"
},
"settings":{
"invoiceDisabled":"true"
},
"products": [
{
"name": "Wireless Mouse for Laptop",
"unitPrice": "15000",
"quantity": "1"
},
{
"name": "HDMI cable",
"unitPrice": "6000",
"quantity": "1"
}
]
}
response = requests.post(url, data=json.dumps(data), headers=headers)
However, instead of JSON response, I'm getting HTML. Any ideas why? When using Python 2.7 I've used urllib and it worked but using urllib to make a call in Python 3.7 produces exactly the same effect, that is HTML instead of JSON.
Response should look like
{
"status":{
"statusCode":"SUCCESS",
},
"redirectUri":"{payment_summary_redirection_url}",
"orderId":"WZHF5FFDRJ140731GUEST000P01",
"extOrderId":"{YOUR_EXT_ORDER_ID}",
}
It was enough to add allow_redirects=False.

Resources