using HTTPRequest setPayload in google app engine - google-app-engine

I am trying to do HTTPRequest Post via Google App Engine.
This is what I have so far
URL url = new URL("http://myurl.com/myfile.php");
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
request.setPayload(########);
HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
Here I need to put some paired values (ie. "email","hi#example.com" etc)
Since setPayload accept byte[] I have no idea how to convert my paired values
into byte.
I have searched other posts but I am very stuck.
EDIT:
I have changed to this but it is still not working
byte[] data = ("EMAIL=bo0#gmail.com&TITLE=evolution&COMMENT=comments&PRICE=5000;").getBytes();
try {
URL url = new URL("http://www.bo.x10.mx/nPost.php");
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
request.setPayload(data);
HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
This is what I have on php website.
<?php
include "path/conf.php"; //logging into database works
$tb_name = 'Post';
$EMAIL=$_POST['EMAIL'];
$TITLE =$_POST['TITLE'];
$COMMENT =$_POST['COMMENT'];
$PRICE =$_POST['PRICE'];
if(!isset($EMAIL) || !isset($TITLE ) || !isset($PRICE )|| !isset($COMMENT)){
header('HTTP/1.0 412 Precondition Failed', true, 412);
die('Bad data');
}
$sql="INSERT INTO $tb_name(EMAIL, TITLE, COMMENT, PRICE) VALUES ('$EMAIL', '$TITLE ', '$COMMENT ', '$PRICE ')";
$result=mysql_query($sql);
if($result==TRUE){
echo "successfully inserted into table!";}
else{
echo "error in inserting into table!";
header('HTTP/1.0 500 Internal Server Error', true, 500);}
ob_end_flush();
exit();
?>
EDIT2: This is a working code
try{
byte[] data = ("EMAIL=bo0#gmail.com&TITLE=evolution&COMMENT=comments&PRICE=5000").getBytes("UTF-8");
URL url = new URL("http://www.box.com/nost.php");
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
request.setPayload(data);
HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
}
My database string field is of type UTF-8

You create a String with the request body, and then you get the byte array. For example we have:
URL url = new URL("http://myurl.com/myfile.php");
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
String body = "email=" + email + "&mpla=" + mpla;
request.setPayload(body.getBytes("UTF-8"));
HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
Hope this helps!

Related

Cannot upload a file in blob in sharefile

I am trying to upload a file in Citrix ShareFile via REST API. There are four requests
Get the access_token
Get the folder URI's to decide n which I am going to upload the file.
Get chunk Uri
Upload the file in that chunk Uri
The first three steps work perfectly. When I am trying to POST a request for the no 4. part,
it is creating a file in sharefile but with size of 0.
I have tried that with POSTMAN, and with form-data and the selection of a file, it is working fine. The file is uploaded in sharefile.
The problem arises when I am trying to upload blob data.
I am calling this methods from Salesforce Platform and in Salesforce, I cannot get the physical file in a physical location. I have blob file data stored in salesforce database.
The below part works well.
String fileName_m = 'a12.pdf';
// Get Authentication Token
HttpRequest req = new HttpRequest();
req.setEndpoint('https://{server}.sf-api.com/oauth/token?grant_type=password&client_id={client_id}&client_secret={secret}&username={user_name}&password={password}!');
req.setMethod('GET');
Http http = new Http();
HttpResponse response = http.send(req);
System.Debug(response.getBody());
Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
String token = '';
String refreshtoken = '';
if(response.getStatusCode() == 200){
token = (String)results.get('access_token');
refreshtoken = (String)results.get('refresh_token');
}
system.debug('token=' + token);
// Get Folder ID
HttpRequest req1 = new HttpRequest();
req1.setEndpoint('https://{server}.sf-api.com/sf/v3/Items?$expand=Children');
req1.setMethod('GET');
req1.setHeader('Authorization','Bearer ' + token);
Http http1 = new Http();
HttpResponse response1 = http1.send(req1);
System.Debug(response1.getBody());
Map<String, Object> results1 = (Map<String, Object>)JSON.deserializeUntyped(response1.getBody());
String url = '';
if(response1.getStatusCode() == 200){
url = (String)results1.get('url');
}
System.Debug('Folder Url ' + url);
// Get Chunk URI
HttpRequest req2 = new HttpRequest();
String endPoint = url + '/Upload2';
req2.setEndpoint(endPoint);
req2.setMethod('POST');
req2.setHeader('Authorization','Bearer ' + token);
req2.setHeader('Content-Type', 'application/x-www-form-urlencoded');
String payload1 = 'FileName='+fileName_m;
req2.setBody(payload1);
Http http2 = new Http();
HttpResponse response2 = http2.send(req2);
Map<String, Object> results2 = (Map<String, Object>)JSON.deserializeUntyped(response2.getBody());
String ChunkUri = '';
if(response2.getStatusCode() == 200){
ChunkUri = (String)results2.get('ChunkUri');
}
System.Debug('ChunkUri' + ChunkUri);
But, when I am trying to upload blob file data in sharefile, the API responds with a 200 status code. In sharefile, the file is created, but the size of the file is 0.
// Problem is here, it is not working
// Upload File
Attachment att = [SELECT Id, Body, Name, ContentType FROM Attachment WHERE Id='00P3p00001XNLOHEA5'];
String body = EncodingUtil.base64Encode(att.body);
List<Attachment> cvList = [SELECT Id, Body, Name, ContentType FROM Attachment WHERE Id='00P3p00001XNLOHEA5'];
String attachmentBody = EncodingUtil.base64Encode(cvList[0].Body);
Blob pdfBlob = EncodingUtil.base64Decode(attachmentBody);
system.debug('pdfBlob' + pdfBlob);
HttpRequest req3 = new HttpRequest();
req3.setEndpoint(ChunkUri);
req3.setMethod('POST');
req3.setHeader('Authorization','Bearer ' + token);
req3.setHeader('Content-Length', String.valueOf(attachmentBody.length()));
req3.setHeader('Content-Encoding', 'UTF-8');
req3.setHeader('Content-type', 'application/pdf');
req3.setHeader('Connection', 'keep-alive');
req3.setBodyAsBlob(pdfBlob);
Http http3 = new Http();
HttpResponse response3 = http3.send(req3);
System.Debug(response3.getBody());
How to solve the issue?

Exception in Service NOW attachment API invocation-- Premature end of chunk coded message body: closing chunk expected

I am using HttpClient to send POST multipart request, using the below code
String apiUrl = API_URL_DEV;
String name = API_USER_DEV;
String password = API_PASSWORD_DEV;
String authString = name + ":" + password;
String encoding = Base64.getEncoder().encodeToString(authString.getBytes());
apiUrl = "https://<servicenow host>/api/now/attachment/upload";
try {
Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "multipart/form-data");
Header header2 = new BasicHeader(HttpHeaders.ACCEPT, "application/json");
Header header3 = new BasicHeader(HttpHeaders.AUTHORIZATION, "Basic " + encoding);
List<Header> headers = new ArrayList<Header>();
headers.add(header);
headers.add(header2);
headers.add(header3);
HttpHost proxy = new HttpHost("xxxx", 8080, "http");
CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultHeaders(headers)
.setProxy(proxy)
.build();
HttpPost post = new HttpPost(apiUrl);
String textFileName = "C:/Heena_Code/Test.xlsx";
File file = new File(textFileName);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
StringBody stringBody1 = new StringBody("change_request", ContentType.MULTIPART_FORM_DATA);
StringBody stringBody2 = new StringBody("a81c6a1ddb2948d04af824f4059619a9", ContentType.MULTIPART_FORM_DATA);
builder.addPart("table_name", stringBody1);
builder.addPart("table_sys_id", stringBody2);
builder.addPart("uploadFile", fileBody);
HttpEntity entity = builder.build();
post.setEntity(entity);
// Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = client.execute(post, responseHandler); -----> exception here
System.out.println(responseBody);
Getting exception
Severe: org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:263)
at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:222)
at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:183)
at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:210)
at org.apache.http.impl.io.ChunkedInputStream.close(ChunkedInputStream.java:312)
at org.apache.http.impl.execchain.ResponseEntityProxy.streamClosed(ResponseEntityProxy.java:142)
at org.apache.http.conn.EofSensorInputStream.checkClose(EofSensorInputStream.java:228)
at org.apache.http.conn.EofSensorInputStream.close(EofSensorInputStream.java:172)
at org.apache.http.client.entity.LazyDecompressingInputStream.close(LazyDecompressingInputStream.java:97)
at org.apache.http.util.EntityUtils.consume(EntityUtils.java:90)
at org.apache.http.impl.client.AbstractResponseHandler.handleResponse(AbstractResponseHandler.java:69)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:66)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:52)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:223)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:165)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:140)
I changed my approach and used a MultiPartUtility class to fire request through HTTPUrlConnection passing two form fields in text/plain format and the file in application/octetstream format and binary encoding

HTTP request on Zeppelin

is there any way to make an HTTP request inside a Zeppelin paragraph? e.g.
function get_app_name(){
//var xmlHttp = new XMLHttpRequest();
//xmlHttp.open( "GET", "https://example.com/application/key", true, 'username', 'password');
//xmlHttp.send( null );
URL url = new URL("https://example.com/application/key");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
}
I cannot import any of the resources (e.g. URL) because the interpreter doesn't allow it (mongodb interpreter). Is there any way to make a simple GET request in Zeppelin? I'm trying to fetch data for my tables that is not in the specified db as the other elements.
From HTTP request inside MongoDB
%mongodb
function wget(url){
var tmp = "/tmp";
var id = new ObjectId();
var outFile= tmp+"/wget"+id;
var p = run("wget", "--user=user", "--password=password", "-o log", "--output-document="+outFile,url);
if (p==0){
var result = cat(outFile);
removeFile(outFile);
return result;
} else {
return "";
}
}
url = "https://exampleurl.com/resource"
result = wget(url)
print(result)

Unable to Upload the File into DropBox From Salesforce Using /Files_put API?

I tried to upload the file into Drop Box account From Salesforce Using /files_put Dropbox Api. But am always getting following Error :
[Status=Bad Request, StatusCode=400]{"error": "Body may not be empty"}.
Hereby My Code as follows,
public class DropboxController
{
public Blob FileBody{get;set;}
public DropboxController()
{
}
public PageReference DropAuth()
{
HttpRequest request = new HttpRequest();
request.setMethod('POST');
request.setTimeout(60000); request.setEndpoint('https://apicontent.dropbox.com/1/files_put/auto/Test.txt');
Blob val = csvFileBody;
Blob accesstoken = Blob.valueOf('<redacted>');
String AccToken = 'Bearer ' + EncodingUtil.base64Encode(accesstoken);
request.setHeader('Authorization', AccToken);
request.setBodyAsBlob(val);
System.debug(val);
Http hp = new Http();
HttpResponse response = hp.send(request);
System.debug(' RESP ::: ' +response +''+ response.getBody());
return null;
}
}
Please advise .
Thanks,
Vivek.K

Send emails with attachments in salesforce via sendgrid

I am trying to generate a pdf and attach to an email and send it.
I am using salesforce and sendgrid.
I am able to send emails but the problem is when I try to attach the pdf, the email is with the attachment, but this is broken, the file is not empty, but the pdf says is broken (I think is a problem of conversion)
here is the code
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
PageReference pdf = Page.include_attachment;
pdf.getParameters().put('id', 'xxxxxxxxxxxxxxxxxx');
blob fileBody = pdf.getContentAsPDF();
efa.setBody(fileBody);
efa.setFileName('test.pdf');
efa.setContentType('application/pdf;charset=UTF-8;');
send('xxxxx#xxxx.com','Test','Body Test', fileBody);
I tryied using
pdf.getContentAsPDF();
and
pdf.getContent();
but the result is the same.
Send method
public static void send(String emailRecipient, String emailSubject, String emailBody, Blob att){
Boolean success=true;
//construct the body of the request
String requestBody='';
requestBody += 'to='+EncodingUtil.urlEncode(emailRecipient,'UTF-8');
requestBody += '&from='+EncodingUtil.urlEncode(user.Email,'UTF-8');
requestBody += '&fromname='+EncodingUtil.urlEncode(user.Name,'UTF-8');
requestBody += '&subject='+EncodingUtil.urlEncode(emailSubject,'UTF-8');
requestBody += '&text='+EncodingUtil.urlEncode(emailBody.trim(),'UTF-8');
requestBody += '&html='+EncodingUtil.urlEncode(emailBody.trim(),'UTF-8');
requestBody += '&api_user=xxxxx';
requestBody += '&api_key=xxxxx';
requestBody += '&files[attachment.pdf]=#'+ EncodingUtil.base64Encode(att);
//construct request
HttpRequest req = new HttpRequest();
req.setEndpoint('https://sendgrid.com/api/mail.send.json');
req.setMethod('POST');
req.setBody(requestBody);
try{
//send request
Http h = new Http();
HttpResponse res = h.send(req);
//check response
String status = res.getStatus();
if(status.toUpperCase()=='OK'){
success=true;
}
else{
success=false;
}
}
catch(Exception e){
success=false;
}
}
Thank you
There is now a library/devtoolkit to do this for you. At the time of this writing it is in beta, but it is working. Disclaimer: I work at SendGrid and have just recently developed the library.
sendgrid-apex
You can rewrite your code to:
...
Blob att = pdf.getContentAsPDF();
public static void send(String emailRecipient, String emailSubject, String emailBody, Blob att){
SendGrid sendgrid = new SendGrid('username', 'password');
SendGrid.email email = new SendGrid.Email();
email.addTo(emailRecipient);
email.setFrom(user.Email);
email.setFromName(user.Name);
email.setSubject(emailSubject);
email.setText(emailBody.trim());
email.setHtml(emailBody.trim());
email.addAttachmentStream("attachment.pdf", att);
String response = sendgrid.send(email);
}

Resources