Bluemail - How to set user name and password - ibm-cloud

I call the service from app.js with below details but get response as
"description":"Not authorized"
Note that my username and password are correct but I may not be passing these correctly hence need help.
var options = {
host: 'bluemail.w3ibm.mybluemix.net',
port: 443,
path: '/rest/v2/emails',
method: 'POST',
headers: {
"Content-Type": "application/json; charset=utf8",
"username": "xxxxx",
"password": "xxxxxx"
}
};

Related

ByBit API v5 returning error code 170130: "Data sent for parameter '%s' is not valid." No explanation of what parameter '%s' is

I am trying to send a spot order using a unified account. There is absolutely no info about what this error message means and as far as I can tell from the ByBit v5 API docs I am doing everything correct. I have already established that the signature is formatted properly. This is the request being sent along:
RequestBuilder {
method: POST,
url: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"api.bybit.com",
),
),
port: None,
path: "/v5/order/create",
query: None,
fragment: None,
},
headers: {
"content-type": "application/json",
"x-bapi-api-key": xxxxxxxxxxxxxx,
"x-bapi-timestamp": "1676755850417",
"x-bapi-sign": xxxxxxxxxxxxxxxx,
"x-bapi-recv-window": "5000",
},}
The request body that gets attached when the request is sent:
{"category":"spot","symbol":"ETHUSDT","side":"Sell","order_type":"Market","qty":"0.1"}
This is the Rust code used to send the request:
// Set up the HTTP client
let client = reqwest::Client::new();
let mut headers = HeaderMap::new();
headers.insert("Content-Type", HeaderValue::from_static("application/json"));
headers.insert("X-BAPI-API-KEY", HeaderValue::from_str(&api_key)?);
headers.insert("X-BAPI-TIMESTAMP", HeaderValue::from_str(&timestamp)?);
headers.insert("X-BAPI-SIGN", HeaderValue::from_str(&sign)?);
headers.insert("X-BAPI-RECV-WINDOW", HeaderValue::from_static(recv_window));
let body = serde_json::to_string(&order)?;
// Submit the order
let request = client
.post("https://api.bybit.com/v5/order/create")
.headers(headers.clone())
.body(body.clone());
let response = request.send().await?;
And this is the response received:
Response {
url: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"api.bybit.com",
),
),
port: None,
path: "/v5/order/create",
query: None,
fragment: None,
},
status: 200,
headers: {
"content-type": "application/json; charset=utf-8",
"content-length": "122",
"x-bapi-limit": "20",
"x-bapi-limit-status": "19",
"x-bapi-limit-reset-timestamp": "1676755851552",
"ret_code": "170130",
"traceid": "78d2bec29fead6e40447a63ba1eae5d9",
"timenow": "1676755851556",
"server": "Openresty",
"expires": "Sat, 18 Feb 2023 21:30:51 GMT",
"cache-control": "max-age=0, no-cache, no-store",
"pragma": "no-cache",
"date": "Sat, 18 Feb 2023 21:30:51 GMT",
"connection": "keep-alive",
},}
Had to ensure that it was "orderType" and not "order_type" in the signature string and request body.

Always getting error "requestId is required" when doing POST on quickbooks payment API Apps Script

Im creating a script that will process a credit transaction and I always getting this response:
{
"errors": [
{
"code": "PMT-4002",
"type": "invalid_request",
"message": "requestId is required.",
"detail": "requestId",
"infoLink": "https://developer.intuit.com/v2/docs?redirectID=PayErrors"
}
]
}
Im trying to figure out where to put the "request-id" parameter on the request body. Here is my code:
function QBOcreatecharge(){
var token = "TOKEN"
var service = getQuickbooksService();
if (service.hasAccess()) {
var url = 'https://sandbox.api.intuit.com/quickbooks/v4/payments/charges'
var Details =
{
"amount": "80.00",
"currency": "USD",
"capture": "false",
"token": token
}
var params = {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
contentType: 'application/json',
method: 'POST',
payload: JSON.stringify(Details),
muteHttpExceptions:true
}
var response = UrlFetchApp.fetch(url, params);
var value = JSON.parse(response.getContentText())
Logger.log(value)
}
else{
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s', authorizationUrl);
}
}
How do I add the requestId parameter? I tried to insert it on the link, on the header and nothing work. Im using UrlFetch on Google Apps Script. Any help will be appreciated. Thanks!
The Request-Id is a header you need to send. e.g.:
headers: {
Authorization: 'Bearer ' + service.getAccessToken(),
'Request-Id': your unique value here
},
Intuit documents it here:
https://developer.intuit.com/app/developer/qbpayments/docs/develop/explore-the-quickbooks-payments-api/rest-api-features#identifiers

Google Apps Script doesn't recognize "Host" header in HTTP POST request?

I'm trying to query ArcGIS Rest API from Google Apps script. Building the request in Postman works perfectly fine, but when I get the code into apps script, I'm having trouble that I cant seem to figure out. Here's the code:
function callEsri () {
var url = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/query"
var params =
{
"async": true,
"crossDomain": true,
"url": "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/query",
"method": "POST",
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "PostmanRuntime/7.20.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "[TOKEN]",
"Host": "services3.arcgis.com",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "125",
"Connection": "keep-alive",
"cache-control": "no-cache"
},
"data": {
"f": "json",
"where": "CITY_JUR LIKE '%Los Angeles%'",
"outSr": "4326",
"outFields": "TRL_NAME,ELEV_FT,CITY_JUR,PARK_NAME,FID"
}
}
var response = UrlFetchApp.fetch(url, params);
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data);
}
The Error I am getting is: Execution failed: Attribute provided with invalid value: Header:Host (line 28, file "Code")
Any reason why Google is not recognizing this and is there a way to fix this? Any help/advice is greatly appreciated.
As #TheMaster has already noted in the comments. You are already specifying the Host in the url.
Also you can take a look at the official documentation of URLFetchApp.
And in case you want more information in the head here you have the mozilla documentation on that header and also the RFC specifying the Host header.

Getting POST Request Message using React.JS

I am writing a program which has a "sign up" functionality. The front-end is created using React.JS. So far, I am able to using this code to send a post request in React.JS:
fetch('http://localhost:2000/users/signup/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"email": "testing#gmail.com",
"password": "secret",
"name": "matthew",
"organization": "Apple"
})
}).then(function(response) {
return response.json();
});
This works perfectly - for the user information is now in the database. However I am not sure how to get the response JSON using response.json(). I want to be able to take the response, get the message string, and display it to my user on the front-end. This is the response when I run the same post request on Postman.
{
"message": "New user created"
}
Thanks for the help!
response.json() returns a promise, so you need one more then to get the actual data:
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data);
})
see https://developer.mozilla.org/en-US/docs/Web/API/Body/json

BadRequestHttpException POST REST Resource

I am attempting to create a custom REST resource. My module is as follows:
/example.info.yml
name: Example
description: ''
type: module
core: 8.x
version: DEV
dependencies:
- serialization
- basic_auth
- rest
/src/Plugin/rest/resource/BasicGetResource.php
namespace Drupal\Example\Plugin\rest\resource;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
/**
* Provides an Example Resource
*
* #RestResource(
* id = "example_resource",
* label = #Translation("Example Resource"),
* uri_paths = {
* "canonical" = "/api/basic",
* "https://www.drupal.org/link-relations/create" = "/api/basic"
* }
* )
*/
class ExampleResource extends ResourceBase {
/**
* Responds to GET requests.
* #return \Drupal\rest\ResourceResponse
*/
public function get() {
$response = ['data' => 'Basic Authentication'];
return new ResourceResponse($response);
}
/**
* Responds to POST requests.
* #return \Drupal\rest\ResourceResponse
*/
public function post($data) {
$response = ['data' => $data];
return new ResourceResponse($response);
}
}
/config/install/rest.resource.example_resource.yml
langcode: en
status: true
dependencies:
module:
- basic_auth
- example
- serialization
_core:
default_config_hash: NSa-WWwv2X-ogB4ojX2-m6rCsWMY6tzOZFZySnI5yfM
id: example_resource
plugin_id: example_resource
granularity: resource
configuration:
methods:
- GET
- POST
formats:
- json
authentication:
- basic_auth
Attaching the following Javascript to a Block for POST:
var token;
var credentials = btoa("[USERNAME]:[PASSWORD]");
var settings = {
"async": true,
"crossDomain": true,
"url": "/rest/session/token",
"method": "GET",
"headers": {"Content-Type": "application/x-www-form-urlencoded"},
};
$.ajax(settings).done(function (response) {
token = response;
});
$('#btn-basic-post').click(function() {
var form = new FormData();
form.append("message", "Hello World!");
var settings = {
"async": true,
"crossDomain": true,
"url": "/api/basic?_format=json",
"method": "POST",
"headers": {
"x-csrf-token": token,
"authorization": "Basic " + credentials,
"cache-control": "no-cache",
"Content-Type": "application/json",
"Accept": 'application/json',
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
alert(response.data);
});
});
I receive status 400, with response {"message":"Syntax error"}. The following log is records:
Symfony\Component\HttpKernel\Exception\BadRequestHttpException: Syntax error in Drupal\rest\RequestHandler->handle() (line 101 of C:\Users\bamberj\Sites\devdesktop\drupal-rest\core\modules\rest\src\RequestHandler.php).
Any advice would be much appreciated. Thanks.
https://www.drupal.org/project/drupal/issues/2928340
Data was not encoded correctly ...
var data = JSON.stringify({
message: 'Hello World',
});
var settings = {
"async": true,
"crossDomain": true,
"url": "/api/basic?_format=json",
"method": "POST",
"headers": {
"x-csrf-token": token,
"authorization": "Basic YWRtaW46YWRtaW4=",
"cache-control": "no-cache",
"Content-Type": "application/json",
"Accept": 'application/json',
},
"data": data,
"dataType": "JSON",
}
$.ajax(settings).done(function (response) {
console.log(response);
});
Thanks cilefen. See https://www.drupal.org/project/drupal/issues/2928340#comment-12371675.