Php post request encoding

Php http post encode parameters in php

You didn’t mention any details, but if it’s from inside a document in the browser, you can either use a form: You can make a link that will send the form by javascript: or an XmlHttpRequest with javascript: Solution: That’s not how URL string query parameters are handled by the TIdHTTP class. The encoded parameter string becomes: a=a+a&b=b%2Bb&c=%C3%BC+%C3%B6 Solution 1: and are HTTP methods.

How to send multiple parameterts to PHP server in HTTP post

For the network operation these is better supporting API like AFNetworking available witch work async and way better to handle

Tutorials for AFNetworking

NSArray *keys = @[@"UserID", ]; NSArray *objects = @[@(userId)]; NSDictionary *parameter = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL: [NSURL URLWithString:BaseURLString]]; [httpClient setParameterEncoding:AFJSONParameterEncoding]; [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"services/UserService.svc/GetUserInfo" parameters:parameter]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) < NSError* error = nil; id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error]; if ([jsonObject isKindOfClass:[NSDictionary class]]) < // do what ever >> failure:^(AFHTTPRequestOperation *operation, NSError *error) < >]; 

Given a NSDictionary «params» whose keys and values are strings and where every entry represents one parameter (name/value) you can define a helper category:

@interface NSDictionary (FormURLEncoded) -(NSData*) dataFormURLEncoded; @end 

dataFormURLEncoded returns a properly encoded character sequence from the given parameters in the dictionary.

Читайте также:  Поменять цвет кнопки java

The encoding algorithm is specified by w3c: URL-encoded form data / The application/x-www-form-urlencoded encoding algorithm

It can be implemented as follows:

First, a helper function which encodes a parameter name, respectively a parameter value:

static NSString* x_www_form_urlencoded_HTML5(NSString* s) < // http://www.w3.org/html/wg/drafts/html/CR/forms.html#application/x-www-form-urlencoded-encoding-algorithm , Editor's Draft 24 October 2013 CFStringRef charactersToLeaveUnescaped = CFSTR(" "); CFStringRef legalURLCharactersToBeEscaped = CFSTR("!$&'()+,/:;=?@~"); NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (__bridge CFStringRef)s, charactersToLeaveUnescaped, legalURLCharactersToBeEscaped, kCFStringEncodingUTF8)); return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"]; >

Finally, dataFormURLEncoded composes the character sequence of the encoded parameters. A «parameter» will be composed by concatenating the encoded name , = and encoded value :

Then, the parameter list will be composed by concatenating the parameters by separating them by a «&»:

parameters := parameter ["&" parameter] 

It can be implemented as below:

@implementation NSDictionary (FormURLEncoded) -(NSData*) dataFormURLEncoded < NSMutableData* data = [[NSMutableData alloc] init]; BOOL first = YES; for (NSString* name in self) < @autoreleasepool < if (!first) < [data appendBytes:"&" length:1]; >NSString* value = self[name]; NSData* encodedName = [x_www_form_urlencoded_HTML5(name) dataUsingEncoding:NSUTF8StringEncoding]; NSData* encodedValue = [x_www_form_urlencoded_HTML5(value) dataUsingEncoding:NSUTF8StringEncoding]; [data appendData:encodedName]; [data appendBytes:"=" length:1]; [data appendData:encodedValue]; first = NO; > > return [data copy]; > @end 

Note: The character sequence encodes the strings using Unicode UTF-8.

Example:
NSDictionary* params = @; NSData* encodedParamData = [params dataFormURLEncoded]; 

Now, encodedParamData will be added to your body whose content type is application/x-www-form-urlencoded .

The encoded parameter string becomes:

a=a+a&b=b%2Bb&c=%C3%BC+%C3%B6

Http — Should I URL-encode POST data?, The full data to post in a HTTP «POST» operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ‘;type=mimetype’.

Encoding a POST request

GET and POST are HTTP methods. In GET the request parameters are taken as query string in the request URL. In POST the request parameters are taken as query string in the request body .

So you need to instruct whatever tool you’re using to pass the parameters through the request body instead of the request URL along with a HTTP method of POST instead of (usually the default) GET .

Either way, the parameters just needs to be URL encoded anyway. There’s no difference for POST or GET, unless you’re setting the content encoding to multipart/form-data instead of (usually the default) application/x-www-form-urlencoded .

If you give more details about what programming language and/or library and/or framework you’re using, then we may be able to give a more detailed answer how to invoke a HTTP POST request.

No. The method is not part of the Url. You’d have to make the request in such a way that it uses the post method. You didn’t mention any details, but if it’s from inside a document in the browser, you can either use a form:

You can make a link that will send the form by javascript:

or an XmlHttpRequest with javascript:

var xhr = new XMLHttpRequest(); xhr.open("POST", url); . 

How do I send parameters in an HTTP POST request with, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams

How to post an UTF-8 encoded request to a PHP server with TIdHTTP?

That’s not how URL string query parameters are handled by the TIdHTTP class. The first parameter of the Post method is just the target URL. The URL string query parameters need to be passed as the second parameter of this method as a stream or a string list collection. You need to specify the request encoding in your code as well and the rest will handle the class internally. Try this instead:

var Server: TIdHTTP; Params: TStrings; begin Server := TIdHTTP.Create; try < setup the request charset >Server.Request.Charset := 'utf-8'; < create the name=value parameter collection >Params := TStringList.Create; try < add the name1 parameter (concatenated with its value) >Params.Add('name1=محمد'); < do the request >Server.Post('http://mywebsite.com/insert_studint.php', Params); finally Params.Free; end; finally Server.Free; end; end; 

Http — encoding a POST request, GET and POST are HTTP methods.In GET the request parameters are taken as query string in the request URL. In POST the request parameters are taken as query string in the request body.. So you need to instruct whatever tool you’re using to pass the parameters through the request body instead of the …

What is the best way to send parameters to a subdomain in PHP

The obvious solution here would be to execute the PHP code directly, but assuming that’s not possible for some reason(sub.domain.com is on a different server, perhaps) then you can send data back and forth using cURL

Given that significant amounts of data to a query string can sometimes be problematic, this code uses POST.

In domain.com your code would look something like this:

[1,2,3], 'moreData'=>[4,5,6]]); $payload = ['key'=>$key, 'params'=>$data]; echo "Setting up cURL
"; // I've used sub.domain.com here, but the URL could be anything $ch = curl_init('http://sub.domain.com/processor.php'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); echo "Executing cURL
"; $result = curl_exec($ch); $responseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE ); if ($responseCode != "200") < exit("Error: Response $responseCode"); >echo "Received response
"; $response = json_decode($result); var_dump($response);

The code in sub.domain.com (I’ve called it ‘processor.php’ above) would then be:

 // Simple entry validation. Return a 404 to discourage anyone just poking about if (($_SERVER['REQUEST_METHOD'] !== 'POST') || (empty($_POST['key'])) ) < http_response_code(404); exit; >// Invalid key? return 401 Unauthorised if (!isValidKey($_POST['key'])) < http_response_code(401); exit; >// No params, or not JSON, return 400 Bad Request if (empty($_POST['params']) || (is_null($data=json_decode($_POST['params'])))) < http_response_code(400); exit; >// process data here. Return results header("Content-type: application/json"); $result = ['status'=>'OK','answer'=>'Answer', "data"=>$data ]; echo json_encode($result); 

If everything works, running the domain.com code from a browser should give this result:

Setting up cURL Executing cURL Received response /home/domain.com/html/process.php:28: object(stdClass)[1] public 'status' => string 'OK' (length=2) public 'answer' => string 'Answer' (length=6) public 'data' => object(stdClass)[2] public 'data' => array (size=3) 0 => int 1 1 => int 2 2 => int 3 public 'moreData' => array (size=3) 0 => int 4 1 => int 5 2 => int 6 

Disclaimer: This is proof-of-concept code only. It’s not production-ready, and has had only rudimentary testing.

How do I send a HTTP POST value to a (PHP) page using, The encoding is actually done using urllib, and this supports HTTP POST. There is also a way to use GET, where you have to pass the data into urlencode. Don’t forget to call read() though, otherwise the request won’t be completed. Share. Follow answered Mar 10, 2009 at 21:16. skyronic skyronic. …

Источник

Оцените статью