Saturday, March 4, 2017

Send HTTP Post from VBA-Excel to Laravel

How do I catch an HTTP Post from VBA-Excel in a Laravel controller so that I can update a model? I am running my laravel application on Homestead. Below is a demonstration of what I've tried thus far with no success.

VBA-EXCEL

Sub httpPost()
Dim XMLHTTP As Object
Dim result As String
Dim argumentString As String
argumentString = "screen_name=ET"
Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
XMLHTTP.Open "POST", _
    "http://exceltwitterbot.app:8000/request", False
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
XMLHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
XMLHTTP.send argumentString
MsgBox XMLHTTP.responsetext

Set XMLHTTP = Nothing
End Sub

Web.php

Route::post('/request', [
    'uses' => 'ExcelController@store',
    'as' => 'request.store',
]);

ExcelController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExcelController extends Controller
{
    public function store(Request $request)
    {  
      $name = $request->only('screen_name');
      $excel = new Excelinput;
      $excel->screen_name = $name;
      $excel->save();
    }
}

This is what I see in the MSGBOX in excel... doesn't seem right...

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="robots" content="noindex,nofollow" />
        <style>
            /* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
            html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:text-top;}sub{vertical-align:text-bottom;}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}input,textarea,select{*font-size:100%;}legend{color:#000;}

            html { backg

If i switch the URL in VBA to "http://httpbin.org/post" I get the following response. Does this mean my application does not receive the POST correctly?How do I configure Laravel correctly so that it can accept HTTP POSTs from VBA?

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "screen_name": "ET"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Language": "en-US,en;q=0.5", 
    "Content-Length": "14", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
  }, 
  "json": null, 
  "origin": "24.36.19.160", 
  "url": "http://httpbin.org/post"
}



via Colin Stadig

Advertisement