victordiaz / PHONK

PHONK is a coding playground for new and old Android devices
https://phonk.app
GNU General Public License v3.0
457 stars 27 forks source link

Add allowing generic HTTP POST body #94

Open Slord6 opened 3 years ago

Slord6 commented 3 years ago

As per discussion #92 , it would be useful to be able to have control over the full body of HTTP POST requests.

For example, I have a simple HTTP wrapper around a JSON-backed database, which expects POST requests to contain only the data to be added to the database. At the moment it is therefore not possible to POST data with Phonk to that end-point.

Example code + expected data at discussion #92

victordiaz commented 3 years ago

@Slord6 I uploaded a new version here for you to test https://github.com/victordiaz/PHONK/releases/tag/1.3.3pre Let me know how it goes :)

Slord6 commented 3 years ago

That's working great. Thanks!

SeriousM commented 3 years ago

Is there any chance to get a new release with this enhancements included?

Slord6 commented 3 years ago

I think it was already released. In 1.3.1 looking at the release notes. My last use of it looks like:

const sendData = (data) => {
  data.client = "Phone";
  data.satCount = satCount;
  console.log("Sending: " + JSON.stringify(data));
  network.httpRequest({
   method: 'POST',
  url: url,
  body: {
    type: 'application/json',
    data: JSON.stringify(data)
  }}).onResponse(function (e) {
    console.log(e.status, e.response);
  })
}
victordiaz commented 3 years ago

it should work on 1.40 too unless I screwed up at some point upgrading stuff :)

@Slord6 btw thx! you just made me realize with your code that at some point Mozilla Rhino interpreter got fat arrows and const!!!! I didnt know at all!. On top of that I just realized that toggling a flag we can finally have let and destructuring support :)

I'll release a new version during the weekend, thx for sharing that code!

Slord6 commented 3 years ago

Oh nice, it hadn't even occurred to me they weren't supported so glad it's helpful!

SeriousM commented 3 years ago

@victordiaz thanks for looking into it.

In v1.3.3pre I had the option to use

  network.httpRequest({
    method: 'POST',
    url: 'https://bzzzzzzz.com/bzzzzz',
    body: {
      type: 'application/json',
      data: JSON.stringify([input.text()])
    }
  }).onResponse(function (e) {

but with v1.4.0 I get the error java.lang.IllegalArgumentException: method POST must have a request body. as with previous versions.

I hope you can include the specific code in the upcoming version! Thank you very much for this wonderful framework :)

victordiaz commented 3 years ago

@SeriousM you are right, it does not work currently. It seems I didnt merge properly :sweat_smile: I'll try to upload a new version today or tomorrow fixing the issue.