Formatting XML for Twilio Response - laravel

I am attempting to receive a fax in my Laravel app, but I can't seem to format the XML properly.
Since I am just setting this up for the first time, I tried to emulate the basic how-to in the Twilio Quickstart guide.
Using the Twilio dashboard, I have set a webhook address of myapp.com/fax/sent to be notified when an incoming fax is attempting to be sent:
public function sent()
{
$twimlResponse = new SimpleXMLElement("<Response></Response>");
$recieveEl = $twimlResponse->addChild('Receive');
$receiveEl->addAttribute('action', '/fax/received');
return response($twimlResponse->asXML())
->header('Content-Type', 'text/xml');
}
The error that I get from Twilio is essentially empty:
Error 12100 Document parse failure
The Twilio error request inspector shows no head or body.
I have also tried to return a hard-coded view file in place of building the xml:
view fax.sent:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Receive action="/fax/received" />
</Response>
and then changed the sent function to:
public function sent()
{
return View::make('fax.sent')->header('Content-Type', 'text/xml');
}
This returns the same error message.

Make sure your twiML URL is externally accessible to Twilio (no firewall/IP restrictions, etc) and that it is valid XML with the correct Content-Type.

Related

POST request response being sent in weird format

I intend to send my server response in the following format
Api Doc
I did the following
headersR.add("response_code", "OK");
headersR.add("cmd_code", "SET_FK_NAME");
headersR.add("trans_id", Long.toString(System.currentTimeMillis()/1000000));
JSONPObject map1 = new JSONPObject("fk_name", "jj");
return new ResponseEntity<>(map1, headersR, HttpStatus.OK);
I was getting a negative response from the other side so I checked Wireshark(Had a hard time logging my response body). And I got this in Wireshark.
Wirehark Screenshot
The response body is Definitely not JSON.
How can I fix this?
The response body fk_name("jj") is not JSON, it's JSONP -- Browser would take the function name fk_name and try to execute it with "jj" as parameter.
The root cause is you are using JSONPObject, whose constructor accepts 2 parameters: a function name, and the data value. Not the expected JSON key and value.
To fix this issue and return {"fk_name":"jj"}, remove the JSONPObject stuff and use code as follow:
return new ResponseEntity<>("{\"fk_name\":\"jj\"}", headersR, HttpStatus.OK);

WebAPI and status code 411 "Length Required"

411 Length Required
The request did not specify the length of its content, which is required by the requested resource.
I have the following code:
[HttpPost]
[Route("UploadFileAsync/{RequestID}")]
public async Task<HttpResponseMessage> UploadFileAsync(int RequestID)
{
SetUser();
long maxAllowedFileSize = 9999999;
long? contentLenght = Request.Content.Headers.ContentLength;
if (!contentLenght.HasValue || contentLenght.Value > maxAllowedFileSize)
{
return Request.CreateErrorResponse(HttpStatusCode.LengthRequired, "Please set content lenght and file size should be less than 10 Mb");
}
It works and return 411 status code when size of request is more than 9999999.
But I would like to validate it before uploading the whole request to server (as I understand, sense of this 411 status code to prevent uploading big files if server can't process it). How can I reject request and send 411 status code before sending the whole request to server?
If you want to to validate the size before sending the request to Web API, then you need to do it at Web API client level.
However if you want to perform the validation before the Action in your web api controller is executed, you can use Action Filters. Typically, following steps are involved.
Create custom action filter for the Web API by inherting ActionFilterAttribute class.
Override OnActionExecuting method and write the code to check the content length and return appropriate error code within the method definition.
Register the custom filter in WebApiConfig file.
Decorate the action for which you want to apply this filter with your custom attribute
Refer to this link for step by step implementation.

Returning HTTP 403 substatus from Asp.Net WebApi controller

I'd like to return Http 403 errors from my Asp.Net WebApi controllers when the user does not have permission to perform certain tasks.
However, I'd like to use a substatus on this to give further details about the error, along with the error message.
At the moment, what I get is
HTTP/1.1 403 Read access forbidden
but what I'd like to see is
HTTP/1.1 403.2 Read access forbidden
The code I'm using currently:
[HttpGet]
public EnrollmentDetail Details(int id)
{
var enrollmentDetail = _context.GetEnrollmentDetail(id);
if (!enrollmentDetail.R)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden)
{
ReasonPhrase = "Read access forbidden"
});
}
return enrollmentDetail;
}
I can't find any information any where on how to add these sub-statuses to the response. Is there any way it can be done with the built-in classes? If not, is there a way to write a custom HttpException which could do this for me?
That's because sub-statuses are not part of the HTTP spec and should not be used. If you want to send more details about the problem you encountered, take a look at Json-problem

MVC 3 + REST return custom http error?

In my application I am trying to get it so that when a REST api call is made, if there is an error that it return a proper status code then either Json or Xml in the body of the response.
So 400: { 'ErrorCode': '400', 'Reason' : 'You did something wrong..' }
or 400: <Error><ErrorCode>400</ErrorCode><Reason>You did something wrong</Reason></Error>
However I can't seem to find how to set the status and body to make this happen. Using fiddler inspect whats being passed back and fourth I've found that if I return a normal ActionResult then I can return the body message ok but the status is 200. If I use HttpException then I can set the status code but the body message is returned as a large html document. I've tried using HttpStatusCodeResult but that just seems to fail and return a 302.
I'm a bit stumped.
Try Response.StatusCode = (int)HttpStatusCode.BadRequest; in your action method. Check out this article at develoq for a short tutorial: http://develoq.net/2011/returning-a-body-content-with-400-http-status-code/
Web API can handle this in various ways, but if you want to stick to ASP.NET MVC then use the code below:
Response.StatusCode = 500;
Response.TrySkipIisCustomErrors = true;
return Content("Error description goes here.", "text/plain");
Check out MVC 4 Beta, there is a new feature called Web API that will help you solve this issue.

palm webOS MailService parameter passing problem

i am developing an application in palm webOS. In that application i have to use the MailService to send mail directly without opening any of the email or compose scenes. For that i have to pass params. But i don't know how to pass the params and what params i have to pass to tha MailService.
params.to ="mailId#abc.com";
params.subject = "subj";
params.msg = "message";
this.controller.serviceRequest('palm://com.palm.mail.MailService', {
method: 'messageSend',
parameters: params,
onSuccess: this.messageSentCallback,
onError: this.messageErrorCallback
});
But i am getting error of " Uncaught TypeError: Cannot set property 'to' of undefined,"
can you help me to resolve this problem please.
ThanQ for all.
This looks like a basic Javascript error. Did you put a line like "var params = {};" first to declare the params variable as an empty object?
Do note -- sending email using the service requires that your app access the private system bus as a com.palm.* application. This means you won't be able to distribute via the App Catalog.

Resources