Objective C post request not sending data


ios development

good day. I am trying to send simple post data to the server. This is my code.

 -(void)makeRequest:(NSString*)stringParameters{
    NSError *error;

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURL *url = [NSURL URLWithString:@"http://vaenterprises.webatu.com/Authentication.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];

    [request addValue:@"application/text" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/text" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
     NSString* postData = @"tag=hello&username=yo&something=something";
    [request setHTTPBody:postData];



    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        [self parseJson:data];
    }];

    [postDataTask resume];


}

Looks great until I echo the whole post from the php side like this

echo json_encode($_POST);

I print the result in iOS like this

-(void)parseJson:(NSData*) data{
    NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSError *jsonError = nil;
    NSDictionary* jsonObject= [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
    NSLog(@"%@",myString);
}

The problem with this is that I get empty strings.. so it means no data is sent, this is 10000% objective c side issue, I don't know why this is happening, so in this approach we just use the one that contains the actual string setHttpBody contains key and value separated by &, but you can't see the data sent. So what am I doing wrong? please tell someone

Vishnu gondlekar

Http body must be of type NSData. Try the following code

NSString* stringData = @"tag=hello&username=yo&something=something";
NSData* data = [stringData dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];

Related


Objective C post request not sending data

ios development good day. I am trying to send simple post data to the server. This is my code. -(void)makeRequest:(NSString*)stringParameters{ NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfigu

Objective C post request not sending data

ios development good day. I am trying to send simple post data to the server. This is my code. -(void)makeRequest:(NSString*)stringParameters{ NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfigu

Objective C post request not sending data

ios development good day. I am trying to send simple post data to the server. This is my code. -(void)makeRequest:(NSString*)stringParameters{ NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfigu

Objective C post request not sending data

ios development good day. I am trying to send simple post data to the server. This is my code. -(void)makeRequest:(NSString*)stringParameters{ NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfigu

Objective C post request not sending data

ios development good day. I am trying to send simple post data to the server. This is my code. -(void)makeRequest:(NSString*)stringParameters{ NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfigu

Objective C post request not sending data

ios development good day. I am trying to send simple post data to the server. This is my code. -(void)makeRequest:(NSString*)stringParameters{ NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfigu

Objective C post request not sending data

ios development good day. I am trying to send simple post data to the server. This is my code. -(void)makeRequest:(NSString*)stringParameters{ NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfigu

Objective C post request not sending data

ios development good day. I am trying to send simple post data to the server. This is my code. -(void)makeRequest:(NSString*)stringParameters{ NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfigu

Objective C post request not sending data

ios development good day. I am trying to send simple post data to the server. This is my code. -(void)makeRequest:(NSString*)stringParameters{ NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfigu

Objective C post request not sending data

ios development good day. I am trying to send simple post data to the server. This is my code. -(void)makeRequest:(NSString*)stringParameters{ NSError *error; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfigu

POST request not sending data - fixed

t00n I've been programming in Android Studio for a short amount of time and have been trying to send JSON to an IIS server in a few different ways (Retrofit2, Volley, HttpClient...) but my IIS APP is not getting it right New rows are not introduced in Mysql BB

Error sending POST request with data

beginner I'm trying to create a program that will create an account on this site: https://www.kostkuj.cz/register My main problem is that I don't really know how it works, so I've rebuilt one according to my requirements project. I also tried sending a request

POST request not sending data - fixed

t00n I've been programming in Android Studio for a short amount of time and have been trying to send JSON to an IIS server in a few different ways (Retrofit2, Volley, HttpClient...) but my IIS APP is not getting it right New rows are not introduced in Mysql BB

Error sending POST request with data

beginner I'm trying to create a program that will create an account on this site: https://www.kostkuj.cz/register My main problem is that I don't really know how it works, so I've rebuilt one according to my requirements project. I also tried sending a request

Error sending POST request with data

beginner I'm trying to create a program that will create an account on this site: https://www.kostkuj.cz/register My main problem is that I don't really know how it works, so I've rebuilt one according to my requirements project. I also tried sending a request

Error sending data using POST request in Android

Nikolai Stankovic I always get the same error when sending the username and password to the server. I need to send the data as content-type: form-data because the server doesn't respond to any other type. I tested in postman and it's ok, but when I try to send

Ajax request not sending data via POST

grumpy croton I have the following Javascript function http_post = function (url, data, success) { var json_data = JSON.stringify(data); return $.ajax({ type: "POST", url: url, data: json_data, dataType: "json",

jQuery Ajax not sending multiple data with post request

I am eating I am trying to send back an array or object using jquery post request. I have multiple inputs with the same name like: $.ajax({ type: "POST", url: "/details", date:{ number:$('input[name="number[]"]').val()

Problem sending data via post request in Python

Phantom Fang I'm trying to enter a start and end date for a decision in 2 input boxes on the Gosport Council website by sending a post request. Whenever I print out the text I received after sending the request, it gives me the information that is displayed on

QNetworkAccessManager not sending data part of POST request

Muldosan Cup When sending POST data from the server, everything looks fine from the Qt application, but the data part of the HTTP part is not sent. In Wireshark, the correct "Content-Length" value is visible in the POST packet, but the size of the entire HTTP

Error sending data using POST request in Android

Nikolai Stankovic I always get the same error when sending the username and password to the server. I need to send the data as content-type: form-data because the server doesn't respond to any other type. I tested in postman and it's ok, but when I try to send

Chrome extension post request not sending data

Dillon First time using the site to ask a question. I'm new to Chrome extensions so I'm sure I'm making some stupid mistakes. I apologize in advance. I'm trying to get the url of the current tab, post it to the url, then receive it and push it to the database.

jQuery Ajax not sending multiple data with post request

I am eating I am trying to send back an array or object using jquery post request. I have multiple inputs with the same name like: $.ajax({ type: "POST", url: "/details", date:{ number:$('input[name="number[]"]').val()

Chrome extension post request not sending data

Dillon First time using the site to ask a question. I'm new to Chrome extensions so I'm sure I'm making some stupid mistakes. I apologize in advance. I'm trying to get the url of the current tab, and post it to a url which is then received and pushed to the da

HTML POST request not sending all data

Akaishi Tian I'm building a very basic search engine for a Library database using Node.js + Express.js. My HTML has a POST form that sends the title or keywords to the application in order to query a book in a MySQL database. Searching by title works great, bu

Angular 7 not sending HTTP Post request data

Tanmay Vij I'm trying to send HTTP Post request to a REST API developed with ExpressJS. I am using the following service file to send requests using Angular's HttpClient: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/h

Ajax request not sending data via POST

grumpy croton I have the following Javascript function http_post = function (url, data, success) { var json_data = JSON.stringify(data); return $.ajax({ type: "POST", url: url, data: json_data, dataType: "json",

Error sending data using POST request in Android

Nikolai Stankovic I always get the same error when sending the username and password to the server. I need to send the data as content-type: form-data because the server doesn't respond to any other type. I tested in postman and it's ok, but when I try to send

Angular 7 not sending HTTP Post request data

Tanmay Vij I'm trying to send HTTP Post request to a REST API developed with ExpressJS. I am using the following service file to send requests using Angular's HttpClient: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/h