Asked  7 Months ago    Answers:  5   Viewed   22 times

I am currently using the Chrome Addon Postman - REST Client to easily create POST / GET request.

Now I want to debug an upload script and I am looking for a way to make requests encoded as "multipart/form-data" so that I can send also a file.

 Answers

40

UPDATE: I have created a video on sending multipart/form-data requests to explain this better.


Actually, Postman can do this. Here is a screenshot

Newer version : Screenshot captured from postman chrome extension enter image description here

Another version

enter image description here

Older version

enter image description here

Make sure you check the comment from @maxkoryukov

Be careful with explicit Content-Type header. Better - do not set it's value, the Postman is smart enough to fill this header for you. BUT, if you want to set the Content-Type: multipart/form-data - do not forget about boundary field.

Tuesday, June 1, 2021
 
erotsppa
answered 7 Months ago
69

I suggest you make an array like this:

$time  = (string) (time() - (100 * rand(1,6)));
$photo = file_get_contents($path);

$fields = array(
    array(
        'headers' => array(
            'Content-Disposition' => 'form-data; name="device_timestamp"',
            'Content-Length'      => strlen($time)
        ),
        'body' => $time
    ),
    array(
        'headers' => array(
            'Content-Disposition' => 'form-data; name="photo"; filename="photo"',
            'Content-Type'        => 'image/jpeg',
            'Content-Length'      => strlen($photo)
        ),
        'body' => $photo
    )
);

The method can then look like this:

private function multipart_build_query($fields)
{
    $data = '';

    foreach ($fields as $field) {
        // add boundary
        $data .= '--' . $this->boundary . "rn";

        // add headers
        foreach ($field['headers'] as $header => $value) {
            $data .= $header . ': ' . $value . "rn";
        }

        // add blank line
        $data .= "rn";

        // add body
        $data .= $field['body'] . "rn";
    }

    // add closing boundary if there where fields
    if ($data) {
        $data .= $data .= '--' . $this->boundary . "--rn";
    }

    return $data;
}

You now have a very generic method which supports any kind of field.

Friday, May 28, 2021
 
Sidarta
answered 7 Months ago
21

Take a look at databene benerator, a test data generator that looks close to your requirements.

  • it can generate data for an existing table definition (or even anonymize production data)
  • it can generate larges data set (unlimited size)
  • it supports various input (CSV, Flat Files, DBUnit) and output format (CSV, Flat Files, DBUnit, XML, Excel, Scripts)
  • it can be used on the command line or through a maven plugin
  • it's open source and customizable

I would give it a try.

BTW, a list of similar products is available on databene benerator's web site.

Wednesday, June 9, 2021
 
Freddie
answered 6 Months ago
99

Each of the major software companies that are active in this space offer solution suites that handle name and address parsing, data standardization, record deduplication or matching, record linking/merging, survivorship, and so on. They're all a bit pricey, though.

For example, Oracle's own solution for this problem is the product "Oracle Data Quality (ODQ) for Oracle Data Integrator (ODI)," which is part of their Fusion Middleware stack. As the name implies, ODQ requires ODI (i.e., it is an add-on module that is licensed separately and is dependent on ODI).

IBM's Websphere solution suite (obtained through their Ascential acquisition) includes QualityStage.

Business Objects, now an SAP company, has a Data Quality product under its Enterprise Information Management (EIM) suite.

Other major data quality brands include Dataflux (a SAS company) and Trillium Software (a Harte-Hanks company)

The Gartner Group releases an annual Magic Quadrant for data quality solution suites. Vendors who rate well in these Magic Quadrants usually make the entire report available online to registered users on their website (example 1, example 2).

Sunday, August 1, 2021
 
Bram
answered 5 Months ago
34

So after searching high and low for an answer, and almost giving up, this link finally helped

here is the final working code:

 public HttpResponse invokeXAUTHPOSTService(String url, String token, File file) {

    client = new DefaultHttpClient();

    HttpPost request = new HttpPost(url);

    HttpResponse response = null;

    DRPContentForUpload content = new DRPContentForUpload(file);
    String jsonObject = DRPJSONConverter.toJson(content);
    String BOUNDARY= "--eriksboundry--";

    request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
    request.addHeader("X-AUTHORIZATION",token);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,BOUNDARY,Charset.defaultCharset());
    try {


        entity.addPart("file01", new StringBody(jsonObject));

        entity.addPart("file01", new FileBody(file));

        request.addHeader("Accept-Encoding", "gzip, deflate");

    } catch (UnsupportedEncodingException e) {
        Log.v("encoding exception","E::: "+e);
        e.printStackTrace();
    }
    request.setHeader("Accept", "application/json");
    request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
    request.setEntity(entity);

    try {




        response = client.execute(request);



    } catch (ClientProtocolException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }


    return response;

}
Wednesday, August 4, 2021
 
CodeCaster
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share