How do I do a POST request on Amazon cloudfront API - api

How do I do a POST control request to the Amazon cloudfront API? In the docs it says:
Send a CloudFront control API request that is similar to the following example.
POST /2012-05-05/origin-access-identity/cloudfront HTTP/1.1
[Required headers]
<?xml version="1.0" encoding="UTF-8"?>
<CloudFrontOriginAccessIdentityConfig xmlns="http://cloudfront.amazonaws.com/doc/2012-05-05/">
<CallerReference>20120229090000</CallerReference>
<Comment>Your comments here</Comment>
</CloudFrontOriginAccessIdentityConfig>
I looked around for some information, and I am not sure how to do it. Is it something I can do via a Curl Request like here?
Any help very appreciated! I feel a little lost at the moment.
Many thanks!
Update
I do something like this now, still not working.. Any help?
<?php
$xml = 'POST /2012-05-05/origin-access-identity/cloudfront HTTP/1.1
Host: cloudfront.amazonaws.com
Authorization: [AWS authentication string]
Date: [time stamp]
[Other required headers]
<?xml version="1.0" encoding="UTF-8"?>
<CloudFrontOriginAccessIdentityConfig xmlns="http://cloudfront.amazonaws.com/doc/2012-05-05/">
<CallerReference>ref</CallerReference>
<Comment>The comment.</Comment>
</CloudFrontOriginAccessIdentityConfig>
';
$opts = array(
'http'=>array(
'method'=>'POST',
'header'=>"Content-Type: text/plain\r\n" .
$auth."\r\n",
'content'=>$xml
)
);
$context = stream_context_create($opts);
$url = 'https://cloudfront.amazonaws.com/2012-05-05/origin-access-identity/cloudfront';
$fp = fopen($url, 'r', false, $context);
fpassthru($fp);
$response = stream_get_contents($fp);
print_r($response);
fclose($fp);
?>

FireFox add-on Poster must help.
https://addons.mozilla.org/en-US/firefox/addon/poster/
Other similar tools:
Are there Firefox extension (or any other browser) that allow to send arbitrary POST data to a webpage?

Related

How to send a XML API request in cypress (SOAP API)

I need to send XML requests in cypress but I don't know how to do that. Generally, I worked with cy.request with rest API's. Is it possible to send XML requests in cypress?
How can I do that? Any help would be great!! An example would be lovely!
I left an example request that I use in postman without a problem. But to automate my test I need to send that request in cypress.
(POST request)
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<MT_ServiceOrderInitiaionandMgmt xmlns:prx="***"
xmlns="***">
<VKONTO>***</VKONTO>
<NAME>***</NAME>
<PORTION>***</PORTION>
<AKLASSE/>
<DATUM>***</DATUM>
<Contact>
<RELTYP/>
<NAME>***</NAME>
<TEL_NUMBER>***</TEL_NUMBER>
<PHTYPE>***</PHTYPE>
<SMTP_ADDR>***</SMTP_ADDR>
</Contact>
<LifeSupportIndicator/>
<QMNUM>***</QMNUM>
<QMART>***</QMART>
<MNCOD>***</MNCOD>
<QMTXT>***</QMTXT>
<NOTST>***</NOTST>
<ServiceAddress>
<STREET>***</STREET>
<CITY1>***</CITY1>
<REGIO>***</REGIO>
<LOCATION>***</LOCATION>
<POST_CODE1>***</POST_CODE1>
<HAUS_NUM2/>
</ServiceAddress>
<ABLEINH>***</ABLEINH>
<CO_ASTTX>***</CO_ASTTX>
<STRMN/>
<STRUR>***</STRUR>
<LTRMN/>
<LTRUR>***</LTRUR>
<ERDAT>***</ERDAT>
<MZEIT>***</MZEIT>
<ERNAM>***</ERNAM>
<AEDAT/>
<AEZEIT>***</AEZEIT>
<AENAM/>
<PPSID>***</PPSID>
<ExternalReference>
<ID></ID>
<SYSNAME>***</SYSNAME>
<TYPE>***</TYPE>
</ExternalReference>
<REFNUM/>
<PRIOK/>
<LONT_TXT>***</LONT_TXT>
<PREMS>***</PREMS>
<Meters>
<TPLNR>***</TPLNR>
<VBSARTTEXT>***</VBSARTTEXT>
<TERMSCHL/>
<DLNOT>***</DLNOT>
<STORT>***</STORT>
<KTEXT>***</KTEXT>
<EINBDAT>***</EINBDAT>
<ISPRIM>***</ISPRIM>
<SERNR>***</SERNR>
<MET_SERNR></MET_SERNR>
<HERST>***</HERST>
<TYPBZ/>
<PREISKLA>***</PREISKLA>
<EQKTX>***</EQKTX>
<DSTAT>***</DSTAT>
<MEINS>***</MEINS>
<STANZVORE>***</STANZVORE>
<ISTABLART>***</ISTABLART>
<V_ZWSTAND></V_ZWSTAND>
<E_ZWSTANDO>***</E_ZWSTANDO>
<E_ZWSTANDU>***</E_ZWSTANDU>
<E_VERBERW>***</E_VERBERW>
<ADAT>***</ADAT>
<ATIM>***</ATIM>
<MTU_TPLNR/>
<MTUSN>***</MTUSN> >
<MTU_EQKTX/>
<MTU_EINBDAT>***11</MTU_EINBDAT>
<MTU_SERNR></MTU_SERNR>
<PORT>***</PORT>
</Meters>
</MT_ServiceOrderInitiaionandMgmt>
</soap:Body>
</soap:Envelope>
there are no big differences between standard rest API requests and XML Soap API requests.
cy.request({
method: "POST",
url: "Request URL",
headers: {
"Content-Type": "text/xml;charset=UTF-8",
authorization: "Bearer " + "auth token",
SOAPAction: "serviceOrder",
},
//you have to write your XML body here as a string. I leave a small part
//of my API's body to be an example if someone needs it in the future.
body: '<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><MT_ServiceOrderInitiaionandMgmt xmlns:prx="***" xmlns="***"><VKONTO>5883000</VKONTO><NAME>cypress</NAME><PORTION>MBC87<MTU_EINBDAT>2002-05-11</MTU_EINBDAT><MTU_SERNR></MTU_SERNR><PORT>1</PORT></Meters></MT_ServiceOrderInitiaionandMgmt></soap:Body></soap:Envelope>',
})
.then((res) => {
//Do something with the response.
});

Server not allowing POST method over ssl connection

I am trying to make a POST request to a PHP script that I have deployed on a Pivotal PCF Instance. I have set the essential headers in the PHP script to accept cross domain requests. However, when I make a POST request from the html code shared below, I see that the GET request is sent instead of POST request. In IE 11 browser and I get SCRIPT7002: XMLHttpRequest: Network Error 0x2ef3, Could not complete the operation due to error 00002ef3 error. Also, when I try to post the data from Chrome browser, I see the below request header
Provisional headers are shown
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
It looks like the server is not accepting the POST requests. I am sharing the below server side PHP script and client side html code for reference. I am not sure whether it has something to do with the scripts, server side configurations or SSL certificate.
I have already research a lot and also tried the solution provided in various posts for the similar question on Stackoverflow but none of it helped.
Approaches tried so far,
Approach 1: Using PHP script
Server side PHP script:
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
header('Access-Control-Allow-Credentials: true');
header('Method:' . $_SERVER['REQUEST_METHOD']);
$response = array(
'status' => 0,
'status_message' =>'Employee Addition Failed.'
);
if (isset($_POST) && !empty($_POST)) {
if (!empty($_POST['username']) && !empty($_POST['credentials'])) {
$response = array(
'empid' => 123,
'emp_name' =>'Some Name',
'method' => 'POST'
);
}
}
if (isset($_GET) && !empty($_GET)) {
$id = 0;
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = $_GET['id'];
}
$response = array(
'empid' => 123,
'emp_name' =>'Some Name',
'method' => 'GET',
'id' => $id,
'method' => $_SERVER['REQUEST_METHOD'],
'username' => $_POST['username'],
'enable_post_data_reading' => ini_get('enable_post_data_reading')
);
}
header('Content-Type: application/json');
echo json_encode($response);
?>
Client side html:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#send").click(function(){
$.ajax({
url:"https://ui-php.apps.pcfdev.prod.demo.int/app/?id=1233",
type:"POST",
data:JSON.stringify({username:'Something', credentials: 'credentials'}),
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(){
console.log('Success');
}
});
});
});
</script>
</head>
<body>
<button id="send">Send</button>
</body>
</html>
When I make a call from Chrome browser, I get the below response.
Response Headers from Chrome browser:
When I make a call from IE11 browser, I see the below header being set automatically by the browser and no response is received.
I see the below error in the console of IE11 browser:
Approach 2:
When I try to do a POST curl request, I see that the code inside the GET block from the PHP script gets executed. Below is the response from curl request.
[Note: I have set proxy in the browser, without it, I will not be able to access the PCF API]

Slideshare api getting 'Failed API Validation' error response

I have applied for slideshare API and i got the API and the secret. All i have did is a simple GET request to slideshare which gives me results with the help of a tag.
This is my deluge script which i have tried to call the url using the API.
As per the documentation, i have got the unix time stamp and SHA1 hash.
param = Map();
param.put("api_key","XYZ");
param.put("ts","1565085930");
param.put("hash","xxxxxxxxxxxxxxxxxxxxxxxxx");
param.put("tag","cricket");
request = invokeurl
[
url :"https://www.slideshare.net/api/2/get_slideshows_by_tag"
type : GET
parameters: param
];
info request;
This is the response error i get:
<?xml version="1.0" encoding="UTF-8"?>
<SlideShareServiceError>
<Message ID="1">Failed API validation</Message>
</SlideShareServiceError>
Thank you.
Looks like the API does not work with a GET request. Try the same using a POST request and it should work. The same failed with POSTMAN and it worked only after the request type was changed to POST.
param = Map();
head = Map();
param.put("api_key","XXXXXXXX");
param.put("ts",1577955246);
param.put("hash","b3f3f803XXXXXXXXXXXXXXXX8be21d");
param.put("tag","cricket");
request = invokeurl
[
url :"https://www.slideshare.net/api/2/get_slideshows_by_tag"
type : POST
parameters: param
];
info request;
Response:
<?xml version="1.0" encoding="UTF-8"?>
<Tag>
<Name>cricket</Name>
<Count>0</Count>
</Tag>

How can i call Shopify's API to create a new product?

I need to call Shopify's API's to create a new product or get details of existing products. How can i do this. Basically how to make a connection of salesforce with shopify?
Shopify provide REST API, so you need to use it for integration with your Salesforce solution. On Salesforce side you need to do few steps for implementation of connection.
Usual you need to make a post/get requests to some URL you would do the following.
Add your domain under Remote Site Settings
To send an outbound calls (POST/GET requests) from Apex in Salesforce, you need to add the domain to Remote Site Settings in setup.
Create and send the request in your APEX controller
For instance, for POST request you need to implement something like this
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint('https://someurl.com/api');
req.setMethod('POST');
//these parts of the POST you may want to customize
req.setCompressed(false);
req.setBody('key1=value1&key2=value2');
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
try {
res = http.send(req);
} catch(System.CalloutException e) {
System.debug('Callout error: '+ e);
}
System.debug(res.getBody());
For more information take a look on Salesforce documentation:
Apex Web Services and Callouts
HttpRequest Class
HttpResponse Class
I just recently actually managed to develop what (I think) you are looking for in PHP and since i had plenty trouble finding all the info I actually went ahead and created an account just to answer this for you :)
my PHP code is as follows:
//Modify these
$API_KEY = 'yourAPIkey';
$SECRET = 'yourAPIsercer(password)';
$TOKEN = 'zzz';
$STORE_URL = 'yourstore.myshopify.com';
$url = 'https://' . $API_KEY . ':' . $SECRET . '#' . $STORE_URL . '/admin/products.xml';
$xmlsrc = <<<XML
<?xml version='1.0' encoding='UTF-8'?>
<product>
<title>TITLEH!!!</title>
<body-html>asdasdfda</body-html>
<product-type>Photoshop</product-type>
<variants type="array">
<variant>
<price>3.00</price>
<inventory-quantity>7</inventory-quantity>
</variant>
</variants>
<vendor>JLH</vendor>
</product>
XML;
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($session, CURLOPT_POSTFIELDS, $xmlsrc);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
if(ereg("^(https)",$url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($session);
curl_close($session);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = true;
$doc->formatOutput = true;
$doc->loadXML($result);
echo $doc->saveXML();
I believe it's straightforward enough.
It's also quite easy to modify it for different actions once you get the hang of it...
I got alot of useful info from this thread: https://ecommerce.shopify.com/c/shopify-discussion/t/php-api-example-26017
and the rest i deducted from the shopify API reference: https://help.shopify.com/api/reference/product
(note that API reference uses json examples and i am working with XML)
I hope this helps you.

Generate Access Token in Bigcommerce

I have an issue when trying to generate Access Token by POST data to https://login.bigcommerce.com/oauth2/token. There is an exception error ('The remote server returned an error: (400) Bad Request.'). I don't know why but I already read the document at https://developer.bigcommerce.com/apps/callback#token
If I open that URL on any web browsers. It said that "The page you were looking for doesn't exist."
Could you please help me this?
Thank you,
Trung
If you are getting a 400 response to your POST request to https://login.bigcommerce.com/oauth2/token then that is indicating a problem with your data. The most likely causes are:
You are not including the following header in your POST request:
Content-Type: application/x-www-form-urlencoded
You are not URL encoding your POST data such as the following example:
client_id=236754&client_secret=m1ng83993rsq3yxg&code=qr6h3thvbvag2ffq&scope=store_v2_orders&grant_type=authorization_code&redirect_uri=https%3A%2F%2Fapp.example.com%2Foauth%26context%3Dstores%2Fg5cd38&context=stores%2Fabc123
Also note that the error response message body that you receive should have some more details about the source of the problem.
If you have confirmed the above points then maybe try giving a sample of your POST data or some information about what you are doing to URL encode your data. Make sure not to include your actual client ID, client secret, or redirect URI.
Try ussing cURL
$data = array( "client_id" => "sdfgdfgdfkxddfgdfgdfdfgdfgddfgdfg2",
"client_secret" => "sdfgsdfgsdfgsdfgsdfgdf",
"redirect_uri" => "https://youapp.com/oauth",
"grant_type" => "authorization_code",
"code" => $_GET["code"], "scope" => $_REQUEST["scope"], "context" => $_GET["context"], );
$postfields = http_build_query($data);
$ch = curl_init();
$url = "https://login.bigcommerce.com/oauth2/token";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
$obj = json_decode($output);
var_dump($obj);
Firstly you need to get temporary authorization code, but sending GET request to https://login.bigcommerce.com/oauth2/authorize with parameters clientId, Scope, Context ("stores/{your_store_hash}") and redirect_url.
Only after this, you can change your temporary token to permanent token (see previous post).
This permanent token expires in 30-60 days, but I don't know how to renew it automatically without user action. If you know that, please write how.