I have an api endpoint that I'm receiving data via a POST. My controller signature looks like this:
public function handle(Request $request)
When I go to test my endpoint, I'm running a really basic test like this:
curl -X POST -H 'Content-Type: text/xml' -d '<XML>data</XML>' http://URL/api
When I \Log::debug($request) I get nothing. Even if I \Log::debug($_POST) I still don't get anything.
Is there a filter that's turned on by default in Lumen? I'm kind of at a loss here. Maybe my curl statement is wrong?
You are sending the XML in the request body. Therefore, to retrieve the content of the request, you have to use $request->getContent like this:
public function handle(Request $request)
{
\Log::debug($request->getContent());
}
Related
I need help on my Laravel API,
I have an external API Request written in C#
Method: Post
Data is JSON which is being posted to my Laravel API URL: http://localhost/project/api/company-data
API route:
Route::post('company-data', 'API\APIController#index');
Here is my API Controllers index() function
public function index(Request $request)
{
return var_dump($request->all());
}
My API Call is hitting the function since I was able to capture it on logs,
However it is returning me an empty array?
How do I catch the json data that was posted to my API?
Thank You for your help, my apologies if I cannot explain my concern clearly,
If found the solution,
It is $Request should be
json_decode($request->getContent(), true);
instead of
$request->all()
I have a SpringBootTest class which uses SpringRunner:
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT)
public class DemoApplicationTests {
#Test
public void testCustomerList() {
get("http://localhost:8080/list")
.then()
.assertThat()
.statusCode(200)
.body("size()", is(2));
}
}
I'd like to change the accept header for the test, much like I'd do with curl:
curl --header "Accept: application/json" curl http://localhost:8080
I've tried with:
get("http://localhost:8080/list").contentType(MediaType.APPLICATION_JSON)
However I get the error "ContentType cannot be applied to Response options".
Can you recommend a way to fix it?
Thanks!
You can add accept header using accept method:
.accept(MediaType.APPLICATION_JSON_VALUE)
See also accept vs content type
Accept header is used by HTTP clients to tell the server what content types they'll accept. The server will then send back a response, which will include a Content-Type header telling the client what the content type of the returned content actually is
Seems I've figured it out.
As get is io.restassured.RestAssured.get (my apologies for not specifying it),
then it should be:
given()
.contentType("application/json").
.get("http://localhost:8080/list")
You need to specify the accept header.
get("http://localhost:8080/list")
.accept(MediaType.APPLICATION_JSON_VALUE)
.then()
.assertThat()
.statusCode(200)
.body("size()", is(2));
With Spring REST the request parameters are converted to an object in case you do a post, and you use #RequestBody, for example:
#RestController
#RequestMapping("/reservations")
class ReservationController{
...
#RequestMapping(value="/postByName")
public Reservation save(#RequestBody Reservation reservation) {
return reservationRepository.save(reservation);
}
...
}
Then I do, and this works fine, a Reservation is created:
curl -i -X POST -H "Content-Type:application/json" -d "{ \"name\" : \"Foo\" }" http://localhost:8080/reservations/postByName
My question is if something exists when you use path variables instead of request parameters. So I should do something like:
curl -i -X POST -H "Content-Type:application/json" http://localhost:8080/reservations/postByName/Foo
Now I do it by hand: in the code I create a Reservation with new and put the path variables in it.
The documentation says:
A #PathVariable argument can be of any simple type such as int, long, Date, etc. Spring automatically converts to the appropriate type or throws a TypeMismatchException if it fails to do so. You can also register support for parsing additional data types. See the section called “Method Parameters And Type Conversion” and the section called “Customizing WebDataBinder initialization”.
I know it's possible to send post with json body in alert notification(like here Bosun send alert specifc data via json post body)
But since post with json is now deprecated in Influx I wonder if it's possible to send a post with --data-binary to write a status to specific influx series whenever an alert occurs. Writing to influx with post:
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
more on writing to influx: https://influxdb.com/docs/v0.9/guides/writing_data.html
Thanks in advance
Have you tried just setting the body of the post notification? When testing the curl command you listed above I believe it is still using content-type application/x-www-form-urlencoded which is the default for notifications. I think this would work:
notification influx{
post = http://localhost:8086/write?db=mydb
body = cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000
}
If you need to customize the values you would have to get the subject to render the correct text and use {{.}} to inject it into the post notification.
template influx.testing {
subject = `
{{ if .IsEmail }}
normal email template details here
{{else}}
{{.Group.host}} value={{.Eval .Alert.Vars.value_variable | printf "%.2f"}} {{.State.Touched.UnixNano}}
{{end}}`
body = `email body details here`
}
notification influx{
post = http://localhost:8086/write?db=mydb
body = cpu_load_short,region=us-west,host={{.}}
}
The post subject would be server01 value=0.64 1434055562000000000 assuming $value_variable is an expression that returns the 0.64 value.
also note when testing in the rule page last touched is always 0 so the timestamp would be -6795364578871345152 but it should work correctly for actual alerts.
I am trying to call the ValuesController WebApi controller (that gets created by default) "PUT" method using cURL. No matter what I do, send value="abc", "abc" or "=abc" as I saw suggested in other questions for the POST method, to no avail.
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
I also tried changing the Content-Type to application/json and application/x-www-form-urlencoded, nothing seems to work.
curl -H -d "=TEST" -X PUT http://localhost:30960/api/values/5
Is this a bug in ASP.NET 5 Web Api or is there a different format I need to use when calling?
In order to make it work you need to pass it as a simple string:
"any value here"
There is even a test for this behaviour
Remember to add an application/json Content-Type header when issuing a request.
I've just tested it for both PUT and POST requests.
Using curl it's:
curl -H "Content-Type: application/json" -d "'Test'" -X PUT http://localhost:[port]/api/values/5
If you are posting data as application/x-www-form-urlencoded then you need to remove the FromBody attribute. This is a change in behavior from MVC5/WebAPI2.
If you are posting data as application/json, then you need the FromBody and also a bindable typ. Example:
public class Person
{
public string Name {get; set;}
}
for data { "Name" : "James"}