Libin
Libin

Reputation: 2462

Attaching an image and posting to a JSON API

How can I create a post with an image and attach it as a featured image from iPhone app?

I am using wordpress with JSON API plugin

My url to create post: <-- Reference to this

http://www.example.com/api/create_post/?nonce=eea4bb4ce5&status=publish&title=Test&content=test%20content&categories=animals

Now I need to attach an image from the phone gallery to this url. How can I do this?

Upvotes: 2

Views: 5560

Answers (2)

iOS Developer
iOS Developer

Reputation: 1723

Try this. Use JSON POST method. You can append all your parameters like this.

-(void)uploadImage
{

    NSString *method =  @"";
    NSString *username   = @"";
    NSString *token   = @"";

    NSString *urlString = [NSString stringWithFormat:@"%@",@"yoururl"];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSMutableData *body = [NSMutableData data];

    // file
    NSData *imageData = UIImageJPEGRepresentation( imageView.image, 0.8);
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    //  parameter username
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[username dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    //  parameter token
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"auth_token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[token dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    //    //  parameter method
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[method dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    // close form
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"%@",returnString);
    [returnString release];
}

Hope this may help you.

Upvotes: 3

iDhaval
iDhaval

Reputation: 7844

To full fill your requirement you need to send image file on web-service using by POST method

First chose image from Photo Library or take it from the camera and make data of that Image using following code.

   NSString *imgName = [NSString stringWithFormat:@"ImageTest"];
   NSData *imgData=UIImagePNGRepresentation(imgView.image);

   NSString* urlstring = [NSString stringWithFormat:@"http://www.example.com/api/create_post/?nonce=eea4bb4ce5&status=publish&title=Test&content=test%20content&categories=animals&imageFile=%@",imgName];
   NSLog(@"URL >> %@",urlstring);

   NSURL url = [NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    //===============================================        
    NSMutableURLRequest postRequest = [[[NSMutableURLRequest alloc] init] autorelease];
    [postRequest setURL:url];
    [postRequest setHTTPMethod:@"POST"];

    NSString boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData  *body = [[NSMutableData alloc] init];
    [postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

imgName this is very important object in this code it's image which you need to pass in your web service which is shown as above.

        [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.png\"\r\n",imgName]] dataUsingEncoding:NSUTF8StringEncoding]]; //img name
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

        // Add Image imgData is Declare as Above.
        [body appendData:[NSData dataWithData:imgData]];



    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postRequest setHTTPBody:body];

    NSURLResponse *response;
    NSError error = nil;


    NSData* data = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
    NSString *result=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  //  NSString * jsonRes = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];


    NSLog(@"result = %@",[result JSONValue]);

It perfectly working with me ! I hope you may get help from here!

Upvotes: 1

Related Questions