0

I'm building a REST API Service using PHP. I know how to receive file in standard html form(multipart/form-data).

But in my service I want to use json. So the question is, how to upload file when the client use Content-Type:application-json? How to embed a file in JSON?

Or is there some other or standard way that I must follow regarding REST API design?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Ahmad
  • 4,224
  • 8
  • 29
  • 40

2 Answers2

2

You'll have to encode your file as a string (e.g. in base64) and put this string in your JSON as a JSON value.

Aurélien Bénel
  • 3,775
  • 24
  • 45
  • Thanks for your answer. btw, is there any REST API provider using this method(encode as base64) for uploading file ? – Ahmad Jan 19 '13 at 10:06
  • Well, this is [the way CouchDB handles "inline attachments"](http://wiki.apache.org/couchdb/HTTP_Document_API#Inline_Attachments). – Aurélien Bénel Jan 19 '13 at 10:34
0

Looks like there's a few answers to this question already. See the following question:

How can i upload a file using jquery's $.ajax function with json and php

They largely suggest using a plugin.

Community
  • 1
  • 1
diggersworld
  • 12,770
  • 24
  • 84
  • 119