I'm sending a put/patch request to backend (Inertiajs to Laravel). but the parameter bag is empty and all the inputs shows up in content attribute of request object.
// in frontend [Inertiajs]
form.patch(route('posts.update', props.post.id), options);
// form.put(route('posts.update', props.post.id), options); // same as patch method
// in backend Laravel
dd(request());
result:
Illuminate\Http\Request :
request:
parameters: []
content:
------WebKitFormBoundaryLAUcPkq3YTjgwsCM
Content-Disposition: form-data; name="title"
"some title"
now dd(request('title')) give me null.
what should I do to the code for sending the inputs through request parameters?
There is some multipart limitations. As Inertia says here, uploading files using a multipart/form-data request is not natively supported in some languages for the put, patch or delete methods. The workaround here is to simply upload files using post instead.
In your case, would look something like this:
form.post(route('posts.update', {
_method: 'put',
postId: props.post.id,
})
* Do it like this if you want to preserve the scroll:
form.post(route('posts.update', {
_method: 'put',
postId: props.post.id,
}, {
preserveScroll: true,
})
Related
I am creating a gem to implement the Calendly API and I need to make a request like
curl --header "X-TOKEN: <your_token>"
--data "url=https://blah.foo/bar&events[]=invitee.created"
https://calendly.com/api/v1/hooks
to create a webhook.
As you may see, events is passed as an array of strings and this is indicated by the [].
I am trying to use RestClient::Request#execute to send this request, but I don't know how to pass this array of strings in this case.
I tried
RestClient::Request.execute(method: :post,
url: #uri,
params: { url: url,
events: "invitee.#{type}"
},
headers: { "X-TOKEN": "#{#api_key}" })
but then I am not sending an array as the API expects.
I also tried
RestClient::Request.execute(method: :post,
url: #uri,
params: { url: url,
'events[]': "invitee.#{type}" },
headers: { "X-TOKEN": "#{#api_key}" })
but it didn't work either. I got a bad request error and nothing else.
How should I build my request in this case?
Posted for visibility (previously answered in comment)
RestClient will appropriately serialize a native ruby Array for you meaning
events: ["invitee.#{type}"]
will be serialized into events[]=invitee.created where type == 'created'. This will work for an Array with multiple values too.
events: ["more","than","one"]
will be converted to events[]=more&events[]=than&events[]=one.
Finally it also handles an other data types inside an Array such as another Hash
events: [{first_name: 'Zaphod', last_name: 'Beeblebrox'}]
will become events[][first_name]=Zaphod&events[][last_name]=Beeblebrox
I'm trying to make simple POST request using VueJS to an application which is written in Beego framework (GoLang) but the application doesn't see any input request. Everything is ok when I use standard form request (no ajax).
This is my VueJS code:
storePost: function(event) {
axios.post("/api/posts/store", {body: "test"}).then(function(response) {
if (response.data.status == 200) {
this.posts.push(response.data.data);
}else {
console.log("error");
}
}, function(response){
console.log("error");
});
}
and this is my Beego code:
// router.go
beego.Router("/api/posts/store", &controllers_API.PostsController{}, "post:Store")
// PostsController.go
func (this *PostsController) Store() {
fmt.Println(this.GetString("body"))
// some irrelevant code which handles the response...
}
fmt.Println always prints nothing. When I use standard forms fmt.Println prints the value of the body with no problems.
It seems that Beego only accepts data with this header: 'Content-Type': 'multipart/form-data' so after I added that header everything was ok.
Since I didn't know how to do that with axios I switched to vue-resource and this is the example code which works with Beego:
this.$http.post("/", {test: "test"}, {
emulateJSON: true
});
Now you can print it like this:
fmt.Println(this.GetString("test"))
I hope this helps someone
Just verified that axios and vue-resource use application/json by default. The emulateJSON you use here tells vue-resource to use application/x-www-form-urlencoded. You probably just need to do a json decode in beego because it by default treats request body as urlencoded.
multipart/form-data works probably because it's been there for long(like urlencoded) so beego by default recognizes it. To use vue-resource to post a multipart/form-data request: use FormData. Axios also accepts a FormData as data.
ok.
I'm using Web API to make AJAX requests.
I'm trying to send a PUT request to an action on a controller.
I'm using route attributes.
When I'm sending the data as part of the route data, everything is fine and the action gets the right info.
However, when I'm trying to send the data in the body, I get a 405 status ((Method is not allowed).
I'm also adding the [FromBody] attribute to the parameter. Here's by jQuery call:
type: 'PUT',
url: 'api/ServerQueue/activity',
data: "=2",
success: function (xhr) {
$("#load").hide();
},
error: function () {
$("#load").hide();
}
};
Here's my action:
[Route("status/{status}")]
public string PutStatus([FromBody]int status)
{
}
I placed a "RoutePrefix" on the controller body.
BTW, I'm using VS 2012.
Any idea what could be the source of the problem?
Try changing the route configuration from
[Route("status/{status}")]
to
[Route("status")]
I'm trying to get a crossdomain post to work. I know I can easily use jsonp for GET, but I'm stuck as to how I can implement POST requests.
I've looked up easyXDM, but as I understand it the server also needs some kind of easyXDM implementation, in the form of a "cors" file or something.
Is that true? So if the server does not support it, there's no way to do a crossdomain post (without setting up a proxy, that is)
I've tried it myself with only local files:
<script type="text/javascript">
var xhr = new easyXDM.Rpc(/** The channel configuration*/{
remote: "name.html"
}, {
remote: {
request: {} // request is exposed by /cors/
}
});
</script>
And then do a request like this:
xhr.request({
url: "http://other.domain.be",
method: "POST",
data: {NEWS: "true", IMMO: "true" }
}, function(response) {
alert(response.status);
alert(response.data);
});
But that does nothing.
Yes, easyXDM.Rpc need to be initialized using the server cors url.
xhr = new easyXDM.Rpc({remote: "http://url/cors"}, {remote:{request:{}}});
If you don't want to use easyXDM, you can easily set up the server to accept all requests by adding : (doesn't supported by IE<8)
Header set Access-Control-Allow-Origin *
Header add Access-Control-Allow-Headers X-Requested-With
Header add Access-Control-Allow-Headers X-Request
I'm trying to send a json from the client using the method xhrPost dojo. But I'm getting a 403 errors. Any help?
var str_json = dojo.toJson(arr_markers);
console.log('json elements: '+str_json);
dojo.xhrPost({postData: str_json,
headers: { "Content-Type": "application/json"},
//content:{'prueba': 'HOLA'},
url:'/up_position_elements/',
handleAs: 'text',
load: function(response, ioArgs){alert('response');},
error: function(errorMessage){}
});
And how to read the json in the django view?
Which method should I use?
403 means "forbidden" which means that the view wants a password, cookie, or other form of authentication. Could you show us the view that serves /up_position_elements/ so that we can see what security-related decorators or logic it might contain?