I need to send a byte array of data as a HTTP body using HTTPService class in the Adobe Air API. Can anyone suggest me the way of doing this?
Try this
var encodedString : String = Base64.encode( imageByteArray );
var service : HTTPService = new HTTPService();
service.method = "POST";
service.contentType = 'application/x-www-form-urlencoded';
service.url = 'http://www.mydomain.com/upload.php';
var variables : URLVariables = new URLVariables();
variables.imageArray = encodedString;
variables.variable2 = "some text string";
variables.variable3 = "some more text";
service.send( variables );
Base64 class get from
http://code.google.com/p/jpauclair-blog/source/browse/trunk/Experiment/Base64/src/Base64.as
Related
I want to know how to write my response - filtered Response to a csv file and attach it to an email using Mail kit. I can send an email with a body but I am unable to add an attachment.
//My Object
var result = await _thunderheadReportRepository.GetMembershipOfferDetailsAsync(searchDate, cancellationToken);
var filteredResponse = result.Select(o => new MembershipOfferDetailsResponse { CreationDate = o.CreationDate!, CorrelationId = o.CorrelationId!, PolicyCode = o.PolicyCode!, AnnualPremium = o.AnnualPremium! }).ToList();
return filteredResponse;
//My email body
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress("email", _appSettings.EmailConfiguration.From));
emailMessage.To.AddRange(message.To);
emailMessage.Subject = message.Subject;
var bodybuilder = new BodyBuilder { HtmlBody = string.Format("<h2 style='color:red'>{0}</h2>", message.Content) };
emailMessage.Body = bodybuilder.ToMessageBody();
return emailMessage;
You just need to call:
bodyBuilder.Attachments.Add ("filename.csv");
I'm trying to send some data using the example in the page of onesignal
var request = WebRequest.Create("https://onesignal.com/api/v1/notifications") as HttpWebRequest;
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
request.Headers.Add("authorization", "Basic xxx");
var obj = new
{
app_id = "xxx",
contents = new { en = "English Message" },
included_segments = new string[] { "Active Users" }
};
var param = JsonConvert.SerializeObject(obj);
byte[] byteArray = Encoding.UTF8.GetBytes(param);
This coded works fine, but I'm using Flurl to make a request to onesignal like this:
var body = new
{
app_id = "xxx",
contents = new
{
es = "Mensaje prueba"
},
included_segments = new string[] { "All" }
};
string param = JsonConvert.SerializeObject(body);
var content = new System.Net.Http.ByteArrayContent(Encoding.UTF8.GetBytes(param));
var response = await new Flurl.Url(urlbase)
.AppendPathSegment("notifications")
.WithHeader("Content-Type", "application/json; charset=utf-8")
.WithHeader("Authorization", "Basic xxx")
.PostAsync(content)
.ReceiveString();
but I'm getting the "Bad request". Please someone could help to point how to make the same call with Flurl?
As mentioned in the first comment, you're doing more work than you need to. Flurl will serialize body for you, so remove these lines:
string param = JsonConvert.SerializeObject(body);
var content = new System.Net.Http.ByteArrayContent(Encoding.UTF8.GetBytes(param));
And post body directly using PostJsonAsync:
var response = await urlbase
...
.PostJsonAsync(body)
.ReceiveString();
How to add the header in the WebRequest .
HttpWebRequest tRequest = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "POST";
tRequest.ContentType = "application/json";
var data = new
{
to = devicesId,
notification = new
{
body = "Fcm Test Notification",
title = "Test FCM",
sound = "Enabled"
},
priority = "high"
};
tRequest.Headers["Authorization: key={0}"] = appId;
tRequest.Headers["Sender: id={0}"] = senderId;
i need to add the header to create the web Request.
Thanks
Missing Add in HttpWebRequest is because have missing ISerializable in CoreFx (so you can not simply tRequest.Headers.Add("name", "value");). They working on it and you can follow at https://github.com/dotnet/corefx/issues/12669
As part of a mobile app I'm building I have to authenticate trough the API of Last.FM.
As documented on their website I tried to format to url correctly but appearently I'm doing something wrong because I get error:
Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: https://ws.audioscrobbler.com/2.0/?method=auth.getMobileSession
Last.FM documentation: http://www.last.fm/api/mobileauth
My code below:
var username:String = "xxxxxxx";
var password:String = "xxxxxxxxxxxx";
var api_key:String = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
var secret:String = "xxxxxxxxxxxxxxxxxxxxxx";
var api_sig:String = MD5.hash( "api_key" + api_key + "methodauth.getMobileSessionpassword" + password + "username" + secret);
var request:URLRequest = new URLRequest("https://ws.audioscrobbler.com/2.0/?method=auth.getMobileSession");
var variables:URLVariables = new URLVariables();//create a variable container
variables.username =username;
variables.password = password;
variables.api_key = api_key;
variables.api_sig = api_sig;
request.data = variables;
request.method = URLRequestMethod.POST;//select the method as post/
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, handleComplete);
loader.load(request);//send the request with URLLoader()
Does someone know the answer?
Try to use HTTPService instead of URLLoader. Smth like this:
var http:HTTPService = new HTTPService();
http.useProxy = false;
http.resultFormat = "e4x";
http.method = "POST";
http.url = "https://ws.audioscrobbler.com/2.0/?method=auth.getMobileSession";
var variables:Object = {};
variables.username = username;
variables.password = password;
variables.api_key = api_key;
variables.api_sig = api_sig;
var token:AsyncToken = http.send(variables);
var responder:Responder = new Responder(handleRequestComplete, handleError);
token.addResponder(responder);
Where handleRequestComplete and handleError are your handlers for the request results:
private function handleRequestComplete(event:ResultEvent):void
{
// your code here
}
private function handleError(event:FaultEvent):void
{
// your code here
}
How to post data from a flex file to a php file? I am not able to create an action.
What you need is to create a URLRequest object where you set up your method and data to send. You then start the request with a Loader object.
var req:URLRequest = new URLRequest(yourURL);
req.method = URLRequestMethod.POST;
var vars:URLVariables = new URLVariables();
vars.yourVar = 'yourValue';
req.data = vars;
var ldr:Loader = new Loader();
ldr.load(req);
You need to create an HTTPService in order to send data to a server application like a PHP file from Flex. The data that is going to be sent could be an XML, that way in your PHP file you can parse that XML and get the information that is in it.
I use this function to transform my objects to XML and then send that XML in the HTTPService:
public function objectToXML(obj:Object, root:String):XML {
var qName:QName = new QName(root);
var xmlDocument:XMLDocument = new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
var xml:XML = new XML(xmlDocument.toString());
return xml;
}
That way I create objects with normal properties and don't worry about how to create the XML, then when you are going to send the XML in the HTTPService you just call the method "objectToXML" on the send method of your HTTPService.
You do that like this:
var myData:Object=new Object();
myData.name="Information";
var myService:HTTPService = new HTTPService();
myService.url = "http://example.com/yourFile.php";
myService.method = "POST";
myService.contentType="application/xml";
myService.send(objectToXML(myData,"parent"));