Skip to content

should support multipart/form-data post requests #565

@pbearne

Description

@pbearne

Currently, we need to be a bit creative to attach files to a post request

		// Create a boundary. We'll need it as we build the payload.
		$boundary = md5( time() . $ext );

		// End of Line
		$eol = "\r\n";

		// Construct the payload in multipart/form-data format
		$payload  = '';
		$payload .= '--' . $boundary;
		$payload .= $eol;
		$payload .= 'Content-Disposition: form-data; name="submitted_file"; filename="' . $name . '"' . $eol;
		$payload .= 'Content-Type: ' . $format . $eol;
		$payload .= 'Content-Transfer-Encoding: binary' . $eol;
		$payload .= $eol;
		$payload .= $contents;
		$payload .= $eol;
		$payload .= '--' . $boundary . '--';
		$payload .= $eol . $eol;

		// Create args for wp_remote_request
		$args = array(
			'headers' => array(
				'accept'       => 'application/json',
				'content-type' => 'multipart/form-data;boundary=' . $boundary,
			),
		);

		// Call the standard request function and return it.
		return $this->request( $api_method, $params, $http_method, $payload, $args );

I would like to be able to pass an array of mixed objects and have it be handled as multipart form if it is a POST/PUT/OPTIONS/PATCH call

Describe alternatives you've considered

code like above

Additional context (optional)

  • [X ] I am happy to help create a pull request to implement this feature myself.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions